Cuestionario Ampliado del Censo de Población y Vivienda 2010

El cuestionario ampliado se guarda en un un archivo .RData.

data <- read_sav("D:/CONAPO_Respaldo/CENSO 2010/CENSO 2010/CENSO 2010/Cuestionario Ampliado 2010/Población 2010/Cuestionario Ampliado_2010_Persona.sav")

#data <- data %>%
 #        select(., c(1:96, 101:103))
save(data, 
      file = paste0(here::here(),"/Bases/Censo_Personas_2010.RData"))

Se seleccionan las variables que se desean conservar para la realización de este documento y se guarda en un archivo .RData para practicidad del manejo de datos.

load(paste0(here::here(),"/Bases/Censo_Personas_2010.RData"))

mydata <- data %>%
           select(ENT, NOM_ENT, MUN, NOM_MUN, ENT_MUN, 
                  LNACEDO_C, LNACPAIS_C,
                  RES05EDO_C, RES05PAI_C, MUN05OTR_C,
                  LTRABPAI_C, LTRABMUN_C, ENT_MUN_TRABAJO,
                  EDAD, SEXO, HLENGUA, QDIALECT_C, LI_INALI, PERETN, NIVACAD, ALFABET, 
                  ESTCON, CONACT,  OCUACTIV_C, HORTRA, INGTRMEN, SITTRA, FACTOR, ESTRATO, UPM) %>%
            rename("CVE_ENT" = "ENT",
                   "CVE_MUN" = "ENT_MUN",
                   "MUN_TRAB" = "LTRABMUN_C",
                   "CVE_MUN_TRABAJO" = "ENT_MUN_TRABAJO") %>%
             mutate(CVE_ENT = str_pad(.$CVE_ENT, width = 3, side = c("left"), pad = "0"),
                    CVE_MUN = str_pad(.$CVE_MUN, width = 6, side = c("left"), pad = "0")) %>%
              mutate(CVE_MUN_RES = paste0(.$RES05EDO_C, MUN05OTR_C))

Zonas Metropolitanas 2010

Se anexa la base de datos de las Zonas Metropolitanas 2010 a la base orginal.

ZM_2010 <- read.xlsx(paste0(here::here(), "/Bases/Municipio/ZM_2010.xlsx"), sheet = "ZM_2010") %>%
            select(CVE_ENT, CVE_MUN, CVE_ZM, NOM_ZM) %>%
             mutate(CVE_ENT = stringr::str_pad(.$CVE_ENT, width = 3, side = c("left"), pad = "0"),
                    CVE_MUN = stringr::str_pad(.$CVE_MUN, width = 6, side = c("left"), pad = "0"))

Se asignan las claves de las zonas metropolitanas de acuerdo a las diferentes variables de interes:

  • Residencia hace 5 años

  • Laboral

mydata <- mydata %>%
           # Zonas Metropolitanas por residenicia
           left_join(., ZM_2010 %>% select(-CVE_ENT), by = c("CVE_MUN")) %>%
            # Zonas Metropolitanas en el lugar de residencia hace 5 años
            left_join(., ZM_2010 %>% select(-CVE_ENT) %>% 
                           rename("CVE_ZM_RES" = "CVE_ZM",
                                  "ZM_RES" = "NOM_ZM"), by = c("CVE_MUN_RES" = "CVE_MUN")) %>%
             # Zonas Metropolitanas en el lugar de trabajo
             left_join(., ZM_2010 %>% select(-CVE_ENT) %>% 
                           rename("CVE_ZM_Trabajo" = "CVE_ZM",
                                  "ZM_Trabajo" = "NOM_ZM"), by = c("CVE_MUN_TRABAJO" = "CVE_MUN")) 

save(mydata, file = paste0(here::here(), "/Bases/06_Migracion por Zonas Metropolitanas_2010.RData"))          

✔️A partir de aquí se pueden correr los códidos 👇.

Se carga el archivo Migracion por Zonas Metropolitanas_2010.RData.

load(file = paste0(here::here(), "/Bases/06_Migracion por Zonas Metropolitanas_2010.RData"))

# Para fines prácticos se genera un ponderador de uno 
mydata <- mydata %>%
           select(CVE_ENT, NOM_ENT, CVE_MUN, MUN, NOM_MUN, LTRABPAI_C, MUN_TRAB, CVE_MUN_TRABAJO,
                  CVE_ZM, NOM_ZM, CVE_ZM_TRABAJO, ZM_TRABAJO, EDAD, CONACT, FACTOR, ESTRATO, UPM) %>%
            rename("ENT_PAIS_TRAB" = "LTRABPAI_C") %>%
            mutate(M = 1)

# Se vuelve a cargar la base de datos para fines practicos
ZM_2010 <- read.xlsx(paste0(here::here(), "/Bases/Municipio/ZM_2010.xlsx"), sheet = "ZM_2010") %>%
            mutate(CVE_ENT = stringr::str_pad(.$CVE_ENT, width = 3, side = c("left"), pad = "0"),
                   CVE_MUN = stringr::str_pad(.$CVE_MUN, width = 6, side = c("left"), pad = "0"))

Claves de entidades y municipios
Entidades y Municipios

Se genera un vector con el nombre de las entidades llamado estados para facilitar los filtros en el documento. 

Se genera un vector con las abreviaturas de las entidades llamado ent para fines prácticos. 

Se genera un vector con las claves de los municipios.

# Claves de los estados
estados <- sjlabelled::get_labels(mydata$CVE_ENT)

nom_estados <- c( "Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila de Zaragoza",
                  "Colima", "Chiapas", "Chihuahua", "Ciudad de México", "Durango", "Guanajuato", "Guerrero", "Hidalgo",
                  "Jalisco", "México", "Michoacán de Ocampo", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", "Puebla", 
                  "Querétaro", "Quintana Roo", "San Luis Potosí", "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala", 
                  "Veracruz de Ignacio de la Llave", "Yucatán", "Zacatecas")

est <- c("AGS", "BC", "BCS", "CAMP", "COAH", "COL", "CHIS", "CHIH", "CDMX", "DGO", "GTO", "GRO", "HGO",
         "JAL", "MEX", "MICH", "MOR", "NAY", "NL", "OAX", "PUE", "QRO", "QROO", "SLP","SIN","SON", "TAB", 
         "TAMS", "TLX", "VER", "YUC", "ZAC")

# Claves de los municipios
MUN <- readRDS(paste0(here::here(), "/Bases/municipios_2010.RDS"))
nom_municipios <- sjlabelled::get_labels(MUN$NOM_MUN) %>% as.factor()
municipios <- sjlabelled::get_labels(MUN$CVE_MUN) %>% as.factor()
#saveRDS(MUN, file = paste0(here::here(), "/Bases/municipios_2010.RDS"))

# Claves de las zonas metropolitanas
zm <- sjlabelled::get_labels(mydata$CVE_ZM)[-2]
nom_zm <- sjlabelled::get_labels(mydata$NOM_ZM)[-2]

Movilidad laboral

Movilidad interna

Matrices

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 12 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
      select(CVE_ENT, NOM_ENT, MUN, CVE_MUN, NOM_MUN, ENT_PAIS_TRAB, MUN_TRAB, CVE_MUN_TRABAJO, 
              EDAD, CONACT, CVE_ZM, NOM_ZM, CVE_ZM_TRABAJO, ZM_TRABAJO, FACTOR, ESTRATO, UPM) %>%
        # Se genera una indicadora de zm 
        mutate(I_ZM_2010 = ifelse(is.na(.$CVE_ZM), '0', '1'),
               I_TRAB_ZM_2010 = ifelse(is.na(.$CVE_ZM_TRABAJO), '0', '1')) %>%
        # Se clasifican a los migrantes internos 
        mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_TRABAJO ~ 'Pertenecen a la Zona Metropolitana', #Trabajan en el mismo municipio
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM == .$CVE_ZM_TRABAJO ~ "Pertenecen a la Zona Metropolitana", #Trabajan en otro municipio dentro de la misma zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM != .$CVE_ZM_TRABAJO ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio pero de otra zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio que no pertenece a la zona metropolitana pero viven en una ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Entran a trabajar a la zona metropolitana pero no pertecen a la ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Trabajan en otro municipio que no es ZM y no residen en una ZM
                                )) %>%
         filter((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
          filter(CVE_MUN_TRABAJO %in% municipios) %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

#MC %>%
 #group_by(I_ZM) %>%
  #summarise(Total = format(sum(FACTOR), big.mark   = " ", scientific = FALSE))

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_municipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_municipal.RDS"))

Migrantes <- svytable(~CVE_MUN_TRABAJO + CVE_MUN, design = MC)

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_TRABAJO, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 17, 22)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel municipal 2010.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel municipal 2010.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Reciente")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel municipal 2010.xlsx"), overwrite = TRUE)

Matriz de movilidad laboral a nivel municipal, 2010.

Matriz de movilidad laboral por zonas metropolitanas
Nivel municipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 306788 279 562 4 2524 406 449 0 62 97 1378 0 0 0 33 0 0 0 0 0 0 0 18 0 0 0 0 0 0
001002 798 8854 0 0 64 8 74 0 138 27 218 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 204 0 17419 0 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 220 8 26 2945 42 10 201 0 17 0 154 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 7485 0 265 0 23334 103 102 44 0 0 459 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 989 56 24 24 358 8716 294 128 226 38 1243 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 630 54 27 142 194 194 11750 40 162 0 543 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 144 0 2 6 14 50 32 1748 6 6 22 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 580 33 23 33 92 76 682 5 3186 0 328 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 755 66 8 0 8 0 8 0 8 3181 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 2738 40 8 0 580 488 142 24 66 0 7383 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 0 0 0 0 0 0 0 0 0 0 0 189915 246 298 655 264 0 53 28 0 0 0 0 0 0 0 0 0 0
002002 0 0 0 0 0 0 0 0 0 0 0 184 364575 155 436 54 0 0 0 0 0 0 0 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 57 84 36014 740 21 13 0 7 0 0 0 0 0 0 0 0 0 0
002004 0 0 0 0 0 0 0 0 0 0 0 1695 1252 1013 593340 3626 0 169 311 28 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 231 45 59 4263 30726 0 10 16 0 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0 27645 54 177 66 102 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 30 0 0 0 0 15 24457 15 0 0 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 0 14 0 19 0 141 131 107221 738 82 0 0 0 0 0 0 0 0
003008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 70 0 5 0 416 111494 140 0 0 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 7 7 7 7 7258 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18245 220 258 262 176 110 0 88
004002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 82 102084 1347 470 11 39 0 141
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 85988 0 0 17 72 0
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 154 606 27994 0 0 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 146 205 211 116 7330 50 5 140
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 21 212 111 27 0 12037 7 14
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 52 0 0 0 3051 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 132 28 14 49 3 3 2882
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 33 111 0 0 0 11 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel municipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de las Zonas Metropolitanas
NOM_ZM <- stringr::str_wrap(nom_zm, 100)

## Tomamos las Zonas Metropolitanas con más de 3 municipios que tienen flujos migratorios 
ZM_CF <- ZM_2010 %>%
          group_by(CVE_ZM) %>%
           summarise(Count = n()) %>%
            filter(Count >= 0) %>%
             pull(CVE_ZM)

NOM_ZM_CF <- ZM_2010 %>%
              filter(CVE_ZM %in% ZM_CF) %>%
               distinct(CVE_ZM, NOM_ZM)

ZM <- lapply(1:length(ZM_CF), function(x){
                    ZM_2010 %>% 
                     select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
                      filter(CVE_ZM %in% ZM_CF[x])  %>% 
                       mutate(NOM_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>% 
                        pull(NOM_MUN)
})

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(ZM, Migrantes)

Emigrantes <- Emigrantes_function(ZM, Migrantes)

################################## Filtro ######################################
#p <- data.frame(ZM = ZM_CF,
#                filtro_municipio = tabla_municipios,
 #               filtro_estado = tabla_estados)
#write.table(p, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel municipal.txt"),
 #           col.names = TRUE)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel municipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

################################################################################
################################################################################
tabla1 <- migration_flows_metropolitan(tabla = Migrantes, 
                                       filtro_zm = ZM, 
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       Emigrantes = Emigrantes, 
                                       Inmigrantes = Inmigrantes, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group = "Otros estados")

## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:length(ZM_CF), function(i){
#                                           tabla1[[i]] %>%
#                                            as.data.frame() %>%
#                                             adorn_totals(c("row", "col"), 
#                                                           fill = "-", 
#                                                            na.rm = TRUE, 
#                                                             ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                              select(`Otros estados`) %>%
#                                               slice(nrow(.)) %>%
#                                                mutate(`Otros estados` = .$`Otros estados`/4) %>%
#                                                 pull(`Otros estados`)
#})

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:length(ZM_CF), function(i){
#                              p <- tabla1[[i]] %>%
#                                    as.data.frame() %>%
#                                     select(-c(`Otros estados`)) %>%
#                                      slice(-nrow(.))
#                              if(sum(p) == 0) {
#                                return(0)
#                              } else {
#                                p %>% 
#                                 adorn_totals(c("row", "col"), 
#                                                              fill = "-", 
#                                                               na.rm = TRUE, 
#                                                                ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                                  slice(nrow(.)) %>%
#                                                   mutate(Total = .$Total/50) %>%
#                                                    pull(Total)
#                              }
 #                                          
#})

## Se guardan las matrices de movilidad laboral para analizarlos después. 
wb <- createWorkbook()
for(i in 1:length(ZM)){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(ZM_CF[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab a nivel municipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Tabla MTrab a nivel municipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Tabla MTrab a nivel municipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Salen por trabajo", 
                        Emigrantes = "Entran por trabajo")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Salen por trabajo", 
                                  Emigrantes = "%Entran por trabajo") 

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:length(ZM)){
     addWorksheet(wb, paste(ZM_CF[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab a nivel municipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Tabla MTrab a nivel municipal.RDS"))

# Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B")

tabla2 <- color_chord_diagram(tabla1, paleta)
file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab desagregado por ZM.pdf"
 
## Gráficos a nivel municipal 
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#170A3A",
                    transparency = 0.2,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text = c(-0.05, 0.5),
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = c(0, 0, 0, 0))

Etiquetas

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Etiquetas a nivel municipal.pdf"

## Etiquetas a nivel municipal
labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = paste(NOM_ZM_CF[,1], NOM_ZM_CF[,2]))

Gráfico Sankey

## Tomamos las Zonas Metropolitanas con más de 3 municipios con flujos migratorios 
ZM_CF <- ZM_2010 %>%
          group_by(CVE_ZM) %>%
           summarise(Count = n()) %>%
            filter(Count > 2) %>%
             pull(CVE_ZM)

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") 

tabla1 <- lapply(1:length(ZM_CF), function(x){
                                   ZM <- ZM_2010 %>%
                                          select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
                                           filter(CVE_ZM %in% ZM_CF[x])  %>%
                                            mutate(NOM_MUN = paste(.$CVE_MUN, .$NOM_MUN)) %>%
                                             pull(NOM_MUN)
                                    tabla %>%
                                     mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% ZM | .$cn %in% ZM), value, 0)) %>%
                                      filter(value > 0) 
  }
)
p <- lapply(1:length(ZM_CF), function(x){
             tabla1[[x]] %>% 
               ggplot(aes(axis1 = rn, 
                           axis2 = cn, 
                            y = value),  # c("value", "freq", "tasa")
                       reverse = FALSE, 
                        na.rm = TRUE) +
                geom_alluvium(aes(fill = rn),
                               curve_type = "quintic", 
                                color = "transparent", 
                                 alpha = 0.85, 
                                  lwd = 0.001, 
                                   width = 1/5,
                                    reverse = FALSE) +
                  geom_stratum(aes(fill = cn), 
                                color = "white", 
                                 alpha = 0.65,  
                                  lwd = 0.001, 
                                   width = 1/5,
                                    reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3, 
                                      direction = "y", 
                                       nudge_x = -.2,
                                        min.segment.length = unit(1, "lines"),
                                         force = 1,
                                          force_pull = 0,
                                           family = "montserrat",
                                            reverse = FALSE) +
                    geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                     stat = "stratum", 
                                      size = 3,
                                       direction = "y", 
                                        nudge_x = .2, 
                                         force = 1,
                                          force_pull = 0,
                                           family = "montserrat",
                                            reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 1.5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 9, family = "montserrat"),
                                   legend.position = c(1, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.35)) +
                        scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/GSankey de MTrab desagregado por ZM_Absolutos (Intramunicipal).pdf")
ggexport(list = p, width = 14, height = 10, dpi = 400, filename = path)

ZMVM

ChordDiagram
ChorDiagram sin grupos
load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel municipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN), by = c("CVE_MUN")) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN), by = c("CVE_MUN")) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

## Se toma como referencia a la Zona Metropolitana del Valle de México
ZM <- ZM_2010 %>% 
       select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
        filter(CVE_ZM %in% "13.01")  %>% 
         mutate(NOM_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 30)) %>% 
          pull(NOM_MUN)

################################################################################
################################## Filtro ######################################

Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)    

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value > 40000) %>% 
               pull(rn)

filtro_est <- Inmigrantes %>%
               full_join(., Emigrantes, by = c("rn" = "cn")) %>%
                filter(rn %nin% ZM) %>%
                 mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
                  mutate(rn = substr(.$rn, 1, 3)) %>%
                   group_by(rn) %>%
                    summarise(value = sum(value)) %>%
                     filter(value >= 25000) %>% 
                      pull(rn)

################################################################################
tabla1 <- migration_flows_metropolitan_city(tabla = Migrantes, 
                                            filtro_zm = ZM, 
                                            filtro_municipios = filtro, 
                                            filtro_estados = filtro_est, 
                                            category_group = estados, 
                                            category_names = nom_estados,
                                            group = "Otros estados") %>%
           dcast(., rn ~ cn, value.var = "value", sum,  na.rm = TRUE) %>%
            column_to_rownames(., var = "rn") 
# Paleta de colores
paleta <- rev(c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B"))

tabla2 <- color_chord_diagram(tabla1 = as.matrix(tabla1), paleta)

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab de ZMVM (Municipal).pdf"

## Gráficos a nivel zona metropolitana (ZMVM)
chord_diagram_graph(file = file, 
                    width = 7, 
                    height = 7, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#170A3A",
                    transparency = 0.25,
                    circo.text = 7,
                    circos.axis.text = 5,
                    adj.text = c(-0.1, 0.5),
                    adj.ylim = 0.2,
                    gap.degree = 3, 
                    clock.wise = TRUE,
                    track.margin = c(-0.2, 0.2),
                    margin = rep(1.5, 4))
ChordDiagram con grupos
load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel municipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN), by = c("CVE_MUN")) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN), by = c("CVE_MUN")) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

## Se toma como referencia a la Zona Metropolitana del Valle de México
ZM <- ZM_2010 %>% 
       select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
        filter(CVE_ZM %in% "13.01")  %>% 
         mutate(NOM_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>% 
          pull(NOM_MUN)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)  

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value > 40000) %>% 
               pull(rn)

filtro_est <- Inmigrantes %>%
               full_join(., Emigrantes, by = c("rn" = "cn")) %>%
                filter(rn %nin% ZM) %>%
                 mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
                  mutate(rn = substr(.$rn, 1, 3)) %>%
                   group_by(rn) %>%
                    summarise(value = sum(value)) %>%
                     filter(value >= 25000) %>% 
                      pull(rn)
 
################################################################################
tabla <- migration_flows_metropolitan_city(tabla = Migrantes, 
                                           filtro_zm = ZM, 
                                           filtro_municipios = filtro, 
                                           filtro_estados = filtro_est, 
                                           category_group = estados, 
                                           category_names = nom_estados,
                                           group = "Otros estados")  

tabla1 <- tabla %>%
           dcast(., rn ~ cn, value.var = "value", sum,  na.rm = TRUE) %>%
            column_to_rownames(., var = "rn") 

# Grupo 1
grupo1 <- tabla %>%
           filter(substr(.$rn, 1, 2) == "09") %>%
            pull(rn) %>%
             unique()
# Grupo 2
grupo2 <- tabla %>%
           filter(substr(.$rn, 1, 2) == "15") %>%
            pull(rn) %>%
             unique()
# Grupo 3
grupo3 <- tabla %>%
           filter(substr(.$rn, 1, 2) == "13") %>%
            pull(rn) %>%
             unique()
# Grupo 4
grupo4 <- tabla %>%
           filter(substr(.$rn, 1, 2) %nin% c("09", "15", "13")) %>%
            pull(rn) %>%
             unique()

## Se guardan las matrices de movilidad laboral para analizarlos después. 
tabla <- tabla1 %>%
          as.data.frame() %>%
           adorn_totals(c("row", "col"),  
                        fill = "-", 
                        na.rm = TRUE, 
                        ,,,,contains(colnames(tabla1)))

wb <- createWorkbook()
addWorksheet(wb, "ZMVM")
writeData(wb, 1, tabla, colNames = TRUE, rowNames = TRUE)
saveWorkbook(wb, 
              file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab de ZMVM a nivel municipal_Reduccion.xlsx"), 
               overwrite = TRUE)
# Paleta de colores
paleta <- c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B")

tabla2 <- color_chord_diagram(tabla1 = as.matrix(tabla1), paleta)

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab de ZMVM_grupos (Municipal).pdf"

## Gráficos a nivel zona metropolitana (ZMVM)
chord_diagram_graph_zmvm(file = file, 
                         width = 10, 
                         height = 10, 
                         family = "Montserrat Medium", 
                         paleta = paleta, 
                         tabla1 = as.matrix(tabla1), 
                         color_labels = "#170A3A",
                         transparency = 0.1,
                         circo.text = 9,
                         circos.axis.text = 7,
                         adj.text = c(-0.01, 0.5),
                         adj.ylim = 1,
                         gap.degree = 3, 
                         clock.wise = TRUE,
                         track.margin = c(-0.2, 0.2),
                         margin = rep(0, 4), 
                         group1 = grupo1, 
                         group1.text = "Ciudad de México",
                         group1.col = 1, 
                         group2 = grupo2, 
                         group2.text = "México",
                         group2.col = 15, 
                         group3 = grupo3, 
                         group3.text = "Hidalgo",
                         group3.col = 20, 
                         group4 = grupo4, 
                         group4.text = "Otro municipios",
                         group4.col = 30) 

Etiquetas

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Etiquetas ZMVM a nivel municipal.pdf"

## Etiquetas a nivel zona metropolitana (ZMVM)
labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = "ZM del Valle de México")
Gráfico Sankey

Zona Metropolitana del Valle de México (ZMVM)

## Se toma como referencia a la Zona Metropolitana del Valle de México
ZM <- ZM_2010 %>% 
       select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
        filter(CVE_ZM %in% "13.01")  %>% 
         mutate(NOM_MUN = paste(.$CVE_MUN, .$NOM_MUN)) %>% 
          pull(NOM_MUN)

##########################################################################################
######################################## Filtro ##########################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)     

######################################## Filtro ##########################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = Inmigrantes + Emigrantes) %>%
              filter(value < 30000) %>% 
               pull(rn)

#########################################################################################
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>% 
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% ZM | .$cn %in% ZM), value, 0)) %>% 
               mutate(rn = ifelse(.$rn %in% filtro, stringr::str_wrap(paste0(substr(as.character(.$rn), 1, 3), " Otros municipios(", estados[as.numeric(substr(as.character(.$rn), 1, 3))], ")"), 20), .$rn),
                      cn = ifelse(.$cn %in% filtro, stringr::str_wrap(paste0(substr(as.character(.$cn), 1, 3), " Otros municipios(", estados[as.numeric(substr(as.character(.$cn), 1, 3))], ")"), 20) , .$cn)) %>%
                filter(value > 0) 

p <- tabla %>% 
      ggplot(aes(axis1 = rn, 
                  axis2 = cn, 
                   y = value),  # c("value", "freq", "tasa")
              reverse = FALSE, 
               na.rm = TRUE) +
       geom_alluvium(aes(fill = rn),
                      curve_type = "quintic", 
                       color = "transparent", 
                        alpha = 0.85,  
                         lwd = 0.001, 
                          width = 1/5,
                           reverse = FALSE) +
         geom_stratum(aes(fill = cn), 
                       color = "white", 
                        alpha = 0.65,  
                         lwd = 0.001, 
                          width = 1/5, 
                           reverse = FALSE) +
           geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                               fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                            stat = "stratum", 
                             size = 3, 
                              direction = "y", 
                               nudge_x = -.23,
                                min.segment.length = unit(1, "lines"),
                                 force = 1,
                                  force_pull = 0,
                                   family = "montserrat",
                                    reverse = FALSE) +
            geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                             stat = "stratum", 
                              size = 3,
                               direction = "y", 
                                nudge_x = .23, 
                                 force = 1,
                                  force_pull = 0,
                                   family = "montserrat",
                                    reverse = FALSE) +
             theme_void() +  
              theme(plot.margin = margin(t = 1, r = 4, b = 1, l = 0, "cm"),
                     text = element_text(family = "montserrat"),
                      axis.text = element_blank(),
                       axis.title = element_blank(),
                        strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                         legend.key.size = unit(0.5, "cm"),
                          legend.text = element_text(size = 9, family = "montserrat"),
                           legend.position = c(0.999, .5)) + 
               scale_x_discrete(expand = c(-0.1, 0.5)) +
                scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                 guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                  labs(fill = "", 
                       color = "")

path = paste0(here::here(), "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/GSankey de MTrab de la ZMVM  (Intramunicipal).pdf")
ggexport(p, width = 20, height = 12, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
##################### Población de 12 años y más ###############################
Pob.ocupada <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.ocupada = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel municipal 2010.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################

## Población que sale de su entidad de residencia y entra a otra demarcación por motivos de trabajo
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_TRABAJO) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para trabajar
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_TRABAJO) %>%
                     group_by(CVE_MUN_TRABAJO) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_TRABAJO") 

tabla <- Pob.Total %>%
          left_join(., Pob.ocupada, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Municipal).xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Municipal).RData"))
Indicadores de movilidad laboral a nivel municipal
Zonas Metropolitanas
CVE_MUN Pob.Total Pob.ocupada Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 793 997 322 616 306 788 9 495 21 310 −11 815 30 805 3.40 7.63 −4.2 −42 620
001002 45 951 12 604 8 854 1 960 714 1 246 2 674 13.39 4.88 8.5 −1 428
001003 53 142 18 510 17 419 370 1 034 −664 1 404 2.07 5.77 −3.7 −2 068
001004 14 302 4 350 2 945 821 281 540 1 102 17.61 6.03 11.6 −562
001005 100 150 35 777 23 334 8 800 4 090 4 710 12 890 25.90 12.04 13.9 −8 180
001006 40 480 13 230 8 716 3 504 1 373 2 131 4 877 26.10 10.23 15.9 −2 746
001007 48 462 15 781 11 750 2 498 2 204 294 4 702 15.55 13.72 1.8 −4 408
001008 7 164 2 114 1 748 288 258 30 546 12.42 11.12 1.3 −516
001009 20 048 5 254 3 186 1 930 711 1 219 2 641 30.51 11.24 19.3 −1 422
001010 18 282 4 668 3 181 937 464 473 1 401 16.33 8.09 8.2 −928
001011 36 822 12 363 7 383 4 196 4 727 −531 8 923 34.12 38.44 −4.3 −9 454
002001 460 793 197 111 189 915 1 827 2 717 −890 4 544 1.11 1.65 −0.5 −5 434
002002 932 001 384 781 364 575 1 835 3 145 −1 310 4 980 0.56 0.96 −0.4 −6 290
002003 95 638 39 811 36 014 1 004 1 567 −563 2 571 2.96 4.63 −1.7 −3 134
002004 1 543 644 650 723 593 340 8 636 8 610 26 17 246 1.57 1.57 0.0 −17 220
002005 91 309 38 424 30 726 4 697 4 009 688 8 706 14.48 12.36 2.1 −8 018
003001 70 358 28 691 27 645 417 215 202 632 1.68 0.87 0.8 −430
003002 58 624 24 829 24 457 75 472 −397 547 0.36 2.26 −1.9 −944
003003 249 303 109 569 107 221 1 199 1 599 −400 2 798 1.34 1.78 −0.4 −3 198
003008 238 498 116 768 111 494 973 1 910 −937 2 883 1.10 2.15 −1.1 −3 820
Fuente: Estimaciones del CONAPO.

Movilidad intramunicipal

Matrices

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 12 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
      select(CVE_ENT, NOM_ENT, MUN, CVE_MUN, NOM_MUN, ENT_PAIS_TRAB, MUN_TRAB, CVE_MUN_TRABAJO, 
              EDAD, CONACT, CVE_ZM, NOM_ZM, CVE_ZM_TRABAJO, ZM_TRABAJO, FACTOR, ESTRATO, UPM) %>%
        # Se genera una indicadora de zm 
        mutate(I_ZM_2010 = ifelse(is.na(.$CVE_ZM), '0', '1'),
               I_TRAB_ZM_2010 = ifelse(is.na(.$CVE_ZM_TRABAJO), '0', '1')) %>%
        # Se clasifican a los migrantes internos 
        mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_TRABAJO ~ 'Pertenecen a la Zona Metropolitana', #Trabajan en el mismo municipio
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM == .$CVE_ZM_TRABAJO ~ "Pertenecen a la Zona Metropolitana", #Trabajan en otro municipio dentro de la misma zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM != .$CVE_ZM_TRABAJO ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio pero de otra zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio que no pertenece a la zona metropolitana pero viven en una ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Entran a trabajar a la zona metropolitana pero no pertecen a la ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Trabajan en otro municipio que no es ZM y no residen en una ZM
                                )) %>%
         filter((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
          filter(CVE_MUN_TRABAJO %in% municipios & .$I_ZM %in% "Pertenecen a la Zona Metropolitana") %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_intramunicipal.RDS"))

Se genera una matriz cruzada de la movilidad laboral a nivel municipal, utilizando la función svytable de la paquetería survey.

MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_intramunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_TRABAJO + CVE_MUN, design = MC)

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_TRABAJO, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 17, 22)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intramunicipal 2010.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intramunicipal 2010.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intramunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intramunicipal 2010.xlsx"), overwrite = TRUE)

Matriz de movilidad laboral a nivel municipal, 2010.

Matriz de movilidad laboral por zonas metropolitanas
Nivel intramunicipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 306788 0 0 0 2524 0 0 0 0 0 1378 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001002 0 8854 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 0 0 17419 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 0 0 0 2945 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 7485 0 0 0 23334 0 0 0 0 0 459 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 0 0 0 0 0 8716 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 0 0 0 0 0 0 11750 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 0 0 0 0 0 0 0 1748 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 0 0 0 0 0 0 0 0 3186 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 0 0 0 0 0 0 0 0 0 3181 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 2738 0 0 0 580 0 0 0 0 0 7383 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 0 0 0 0 0 0 0 0 0 0 0 189915 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002002 0 0 0 0 0 0 0 0 0 0 0 0 364575 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 0 0 36014 740 21 0 0 0 0 0 0 0 0 0 0 0 0 0
002004 0 0 0 0 0 0 0 0 0 0 0 0 0 1013 593340 3626 0 0 0 0 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 0 0 59 4263 30726 0 0 0 0 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 27645 0 0 0 0 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24457 0 0 0 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 107221 0 0 0 0 0 0 0 0 0 0
003008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 111494 0 0 0 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7258 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18245 0 0 0 0 0 0 0
004002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1e+05 0 0 0 0 0 0
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 85988 0 0 0 0 0
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 27994 0 0 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7330 0 0 0
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12037 0 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3051 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2882
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Fuente: Estimaciones del CONAPO.

Matrices por zonas metropolitanas

MR <- NULL
for(i in 1:length(zm)){
tabla <- ZM_2010 %>%
          select(CVE_ZM, CVE_MUN) %>%
           filter(CVE_ZM %in% zm[i]) %>%
            pull(CVE_MUN)

MR[[paste0(zm[i])]] <- Migrantes %>%
                        as.data.frame() %>%
                         tibble::rownames_to_column(var = "CVE_MUN") %>%
                          mutate_if(is.numeric, as.numeric) %>%
                           select(CVE_MUN, tabla) %>%
                            filter(CVE_MUN %in% tabla)
}

# Se guardan en un objeto de R 
saveRDS(MR, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matrices de MTrab a nivel intramunicipal por ZM2010.RDS"))

# Se genera un Excel con todas las matrices por ZM
wb <- createWorkbook()
for(i in 1:length(zm)){
addWorksheet(wb, paste0(zm[i]))
writeData(wb, i, MR[[paste0(zm[i])]] %>% as.data.frame())
saveWorkbook(wb, 
              file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matrices de MTrab a nivel intramunicipal por ZM2010.xlsx"), 
               overwrite = TRUE)
}

Matriz de movilidad laboral en la Zona Metropolitana de Cuernavaca, 2010.

Matriz de movilidad laboral a nivel intramunicipal
Zona Metropolitana de Cuernavaca
CVE_MUN 017007 017008 017009 017011 017018 017020 017024 017028
017007 141187 813 91 4980 841 103 50 345
017008 7865 21221 0 3667 1197 21 21 450
017009 1158 7 5241 70 28 0 0 21
017011 22831 1332 196 53537 740 243 25 429
017018 10891 789 5 751 27337 29 20 744
017020 2565 54 20 1430 77 11634 19 46
017024 1084 699 0 489 79 12 11067 350
017028 6215 682 0 465 1829 47 14 14843
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por zonas metropolitanas

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intramunicipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de las Zonas Metropolitanas
NOM_ZM <- stringr::str_wrap(nom_zm, 100)

## Tomamos las Zonas Metropolitanas con más de 3 municipios que tienen flujos migratorios 
#### Con filtro (CF)
ZM_CF <- ZM_2010 %>%
          group_by(CVE_ZM) %>%
           summarise(Count = n()) %>%
            filter(Count >= 3) %>%
             pull(CVE_ZM)

NOM_ZM_CF <- ZM_2010 %>%
              filter(CVE_ZM %in% ZM_CF) %>%
               distinct(CVE_ZM, NOM_ZM)

ZM <- lapply(1:length(ZM_CF), function(x){
                    ZM_2010 %>% 
                     select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
                      filter(CVE_ZM %in% ZM_CF[x])  %>% 
                       mutate(NOM_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>% 
                        pull(NOM_MUN)
})

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)  

################################# Filtro #######################################
### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
### Filtro <<<<  filter(value > 0 & rn != estado[x])
#p <- data.frame(ZM = ZM_CF,
 #               filtro_municipio = filtro_mig)
#write.table(p, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel intramunicipal.txt"), col.names = TRUE)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel intramunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel intramunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

################################################################################
tabla1 <- intramunicipal_flows_metropolitan(tabla = Migrantes,
                                            filtro_zm = ZM, 
                                            filtro_mig = filtro_mig, 
                                            Emigrantes = Emigrantes,
                                            Inmigrantes = Inmigrantes, 
                                            category_group = estados, 
                                            group = "Otros municipios")

tabla1 <- purrr::map(tabla1, ~ .x %>%
           dcast(rn ~ cn, value.var = "value", sum, na.rm = TRUE) %>%
            column_to_rownames(var = "rn"))

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:length(ZM_CF), function(i){
#                                             tabla1[[i]] %>%
#                                              as.data.frame() %>%
#                                               adorn_totals(c("row", "col"), 
#                                                              fill = "-", 
#                                                               na.rm = TRUE, 
#                                                                ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                                  slice(nrow(.)) %>%
#                                                   mutate(Total = .$Total/50) %>%
#                                                    pull(Total)
#})

## Se guardan las matrices de Movilidad laboral para analizarlos después. 
wb <- createWorkbook()
for(i in 1:length(ZM)){
     if(i %in% 10) {
       tabla <- tabla1[[i]]
        } else {
        tabla <- tabla1[[i]] %>%
                  as.data.frame() %>%
                   adorn_totals(c("row", "col"), 
                                fill = "-", 
                                na.rm = TRUE, 
                                ,,,,contains(colnames(tabla1[[i]])))
        }          
     addWorksheet(wb, paste(ZM_CF[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab nivel intramunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}
saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Tabla MTrab a nivel intramunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Tabla MTrab a nivel intramunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Salen por trabajo", 
                        Emigrantes = "Entran por trabajo")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Salen por trabajo", 
                                  Emigrantes = "%Entran por trabajo") 

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:length(ZM)){
     addWorksheet(wb, paste(ZM_CF[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab nivel intramunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Tabla MTrab a nivel intramunicipal.RDS"))

# Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B")

tabla2 <- color_chord_diagram(tabla1, paleta)
file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab desagregado por ZM  (Intramunicipal).pdf"
 
## Gráficos a nivel intramunicipal 
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#170A3A",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text = c(-0.05, 0.5),
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Etiquetas ZM a nivel intramunicipal.pdf"

## Etiquetas a nivel intramunicipal 
labels_chord_diagram(file, 
                     width = 7, 
                     height = 9, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = paste(NOM_ZM_CF[,1], NOM_ZM_CF[,2]))

Gráfico Sankey

## Tomamos las Zonas Metropolitanas con más de 3 municipios con flujos migratorios 
ZM_CF <- ZM_2010 %>%
          group_by(CVE_ZM) %>%
           summarise(Count = n()) %>%
            filter(Count > 2) %>%
             pull(CVE_ZM)

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") 

tabla1 <- lapply(1:length(ZM_CF), function(x){
                                   ZM <- ZM_2010 %>%
                                          select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
                                           filter(CVE_ZM %in% ZM_CF[x])  %>%
                                            mutate(NOM_MUN = paste(.$CVE_MUN, .$NOM_MUN)) %>%
                                             pull(NOM_MUN)
                                    tabla %>%
                                     mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% ZM | .$cn %in% ZM), value, 0)) %>%
                                      filter(value > 0) 
  }
)
p <- lapply(1:length(ZM_CF), function(x){
             tabla1[[x]] %>% 
               ggplot(aes(axis1 = rn, 
                           axis2 = cn, 
                            y = value),  # c("value", "freq", "tasa")
                       reverse = FALSE, 
                        na.rm = TRUE) +
                geom_alluvium(aes(fill = rn),
                               curve_type = "quintic", 
                                color = "transparent", 
                                 alpha = 0.85, 
                                  lwd = 0.001, 
                                   width = 1/5,
                                    reverse = FALSE) +
                  geom_stratum(aes(fill = cn), 
                                color = "white", 
                                 alpha = 0.65,  
                                  lwd = 0.001, 
                                   width = 1/5,
                                    reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3, 
                                      direction = "y", 
                                       nudge_x = -.2,
                                        min.segment.length = unit(1, "lines"),
                                         force = 1,
                                          force_pull = 0,
                                           family = "montserrat",
                                            reverse = FALSE) +
                    geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                     stat = "stratum", 
                                      size = 3,
                                       direction = "y", 
                                        nudge_x = .2, 
                                         force = 1,
                                          force_pull = 0,
                                           family = "montserrat",
                                            reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 1.5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 9, family = "montserrat"),
                                   legend.position = c(1, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.35)) +
                        scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/GSankey de MTrab desagregado por ZM_Absolutos (Intramunicipal).pdf")
ggexport(list = p, width = 14, height = 10, dpi = 400, filename = path)

ZMVM

ChordDiagram
ChorDiagram sin grupos
load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intramunicipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

## Se toma como referencia a la Zona Metropolitana del Valle de México
ZM <- ZM_2010 %>% 
       select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
        filter(CVE_ZM %in% "13.01")  %>% 
         mutate(NOM_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>% 
          pull(NOM_MUN)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)   

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value < 60000) %>% 
               pull(rn)

################################################################################
tabla1 <- migration_flows_metropolitan_city(tabla = Migrantes, 
                                            filtro_zm = ZM, 
                                            filtro_municipios = filtro, 
                                            filtro_estados = NULL, 
                                            category_group = estados, 
                                            category_names = nom_estados,
                                            group = "ZMVM") %>%
           dcast(., rn ~ cn, value.var = "value", sum,  na.rm = TRUE) %>%                        
            column_to_rownames(., var = "rn")   
#Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B")

tabla2 <- color_chord_diagram(tabla1 = as.matrix(tabla1), paleta)

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab de ZMVM (Intramunicipal).pdf"

## Gráficos a nivel zona metropolitana (ZMVM)
chord_diagram_graph(file = file, 
                    width = 7, 
                    height = 7, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#170A3A",
                    transparency = 0.1,
                    circo.text = 7,
                    circos.axis.text = 5,
                    adj.text = c(-0.1, 0.5),
                    adj.ylim = 0.2,
                    gap.degree = 3, 
                    clock.wise = TRUE,
                    track.margin = c(-0.2, 0.2),
                    margin = rep(1.5, 4))
ChordDiagram con grupos
load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intramunicipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

## Se toma como referencia a la Zona Metropolitana del Valle de México
ZM <- ZM_2010 %>% 
       select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
        filter(CVE_ZM %in% "13.01")  %>% 
         mutate(NOM_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>% 
          pull(NOM_MUN)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)     

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value < 60000) %>% 
               pull(rn)

################################################################################
tabla <- migration_flows_metropolitan_city(tabla = Migrantes, 
                                           filtro_zm = ZM, 
                                           filtro_municipios = filtro, 
                                           filtro_estados = NULL, 
                                           category_group = estados, 
                                           category_names = nom_estados,
                                           group = "ZMVM") 

tabla1 <- tabla %>%
           dcast(., rn ~ cn, value.var = "value", sum,  na.rm = TRUE) %>%
            column_to_rownames(., var = "rn") 

# Grupo 1
grupo1 <- tabla %>%
           filter(substr(.$rn, 1, 2) == "09") %>%
            pull(rn) %>%
             unique()
# Grupo 2
grupo2 <- tabla %>%
           filter(substr(.$rn, 1, 2) == "15") %>%
            pull(rn) %>%
             unique()
# Grupo 3
grupo3 <- tabla %>%
           filter(substr(.$rn, 1, 2) == "13") %>%
            pull(rn) %>%
             unique()

## Se guardan las matrices de movilidad laboral para analizarlos después. 
tabla <- tabla1 %>%
          as.data.frame() %>%
           adorn_totals(c("row", "col"),  
                        fill = "-", 
                        na.rm = TRUE, 
                        ,,,,contains(colnames(tabla1)))

wb <- createWorkbook()
addWorksheet(wb, "ZMVM")
writeData(wb, 1, tabla, colNames = TRUE, rowNames = TRUE)
saveWorkbook(wb, 
              file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab de ZMVM a nivel intramunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
#Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B")

tabla2 <- color_chord_diagram(tabla1 = as.matrix(tabla1), paleta)

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab de ZMVM_grupos (Intramunicipal).pdf"

## Gráficos a nivel zona metropolitana (ZMVM)
chord_diagram_graph_zmvm(file = file, 
                         width = 10, 
                         height = 10, 
                         family = "Montserrat Medium", 
                         paleta = paleta, 
                         tabla1 = as.matrix(tabla1), 
                         color_labels = "#170A3A",
                         transparency = 0.25,
                         circo.text = 9,
                         circos.axis.text = 7,
                         adj.text = c(-0.01, 0.5),
                         adj.ylim = 1,
                         gap.degree = 3, 
                         clock.wise = TRUE,
                         track.margin = c(-0.2, 0.2),
                         margin = rep(0, 4), 
                         group1 = grupo1, 
                         group1.text = "Ciudad de México",
                         group1.col = 1, 
                         group2 = grupo2, 
                         group2.text = "México",
                         group2.col = 15,
                         group3 = grupo3, 
                         group3.text = "Hidalgo",
                         group3.col = 30)

Etiquetas

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Etiquetas ZMVM a nivel intramunicipal.pdf"

## Etiquetas a nivel zona metropolitana (ZMVM)
labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = "ZM del Valle de México")
Gráfico Sankey

Zona Metropolitana del Valle de México (ZMVM)

## Se toma como referencia a la Zona Metropolitana del Valle de México
ZM <- ZM_2010 %>% 
       select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
        filter(CVE_ZM %in% "13.01")  %>% 
         mutate(NOM_MUN = paste(.$CVE_MUN, .$NOM_MUN)) %>% 
          pull(NOM_MUN)

##########################################################################################
######################################## Filtro ##########################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)    

######################################## Filtro ##########################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = Inmigrantes + Emigrantes) %>%
              filter(value < 30000) %>% 
               pull(rn)

#########################################################################################
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>% 
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% ZM | .$cn %in% ZM), value, 0)) %>% 
               mutate(rn = ifelse(.$rn %in% filtro, stringr::str_wrap(paste0(substr(as.character(.$rn), 1, 3), " Otros municipios(", estados[as.numeric(substr(as.character(.$rn), 1, 3))], ")"), 20), .$rn),
                      cn = ifelse(.$cn %in% filtro, stringr::str_wrap(paste0(substr(as.character(.$cn), 1, 3), " Otros municipios(", estados[as.numeric(substr(as.character(.$cn), 1, 3))], ")"), 20) , .$cn)) %>%
                filter(value > 0) 

p <- tabla %>% 
      ggplot(aes(axis1 = rn, 
                  axis2 = cn, 
                   y = value),  # c("value", "freq", "tasa")
              reverse = FALSE, 
               na.rm = TRUE) +
       geom_alluvium(aes(fill = rn),
                      curve_type = "quintic", 
                       color = "transparent", 
                        alpha = 0.85,  
                         lwd = 0.001, 
                          width = 1/5,
                           reverse = FALSE) +
         geom_stratum(aes(fill = cn), 
                       color = "white", 
                        alpha = 0.65,  
                         lwd = 0.001, 
                          width = 1/5, 
                           reverse = FALSE) +
           geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                               fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                            stat = "stratum", 
                             size = 3, 
                              direction = "y", 
                               nudge_x = -.23,
                                min.segment.length = unit(1, "lines"),
                                 force = 1,
                                  force_pull = 0,
                                   family = "montserrat",
                                    reverse = FALSE) +
            geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                             stat = "stratum", 
                              size = 3,
                               direction = "y", 
                                nudge_x = .23, 
                                 force = 1,
                                  force_pull = 0,
                                   family = "montserrat",
                                    reverse = FALSE) +
             theme_void() +  
              theme(plot.margin = margin(t = 1, r = 4, b = 1, l = 0, "cm"),
                     text = element_text(family = "montserrat"),
                      axis.text = element_blank(),
                       axis.title = element_blank(),
                        strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                         legend.key.size = unit(0.5, "cm"),
                          legend.text = element_text(size = 9, family = "montserrat"),
                           legend.position = c(0.999, .5)) + 
               scale_x_discrete(expand = c(-0.1, 0.5)) +
                scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                 guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                  labs(fill = "", 
                       color = "")

path = paste0(here::here(), "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/GSankey de MTrab de la ZMVM  (Intramunicipal).pdf")
ggexport(p, width = 20, height = 12, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
##################### Población de 12 años y más ###############################
Pob.ocupada <- mydata %>%
                as.data.frame() %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
                   group_by(CVE_MUN) %>%
                    summarise(Pob.ocupada = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intramunicipal 2010.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################

## Población que sale de su entidad de residencia y entra a otra demarcación por motivos de trabajo
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_TRABAJO) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para trabajar
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_TRABAJO) %>%
                     group_by(CVE_MUN_TRABAJO) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_TRABAJO") 

tabla <- Pob.Total %>%
          left_join(., Pob.ocupada, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Intramunicipal).xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Intramunicipal).RData"))
Indicadores de movilidad laboral a nivel intramunicipal
Zonas Metropolitanas
CVE_MUN Pob.Total Pob.ocupada Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 793 997 322 616 306 788 3 902 10 223 −6 321 14 125 1.40 3.66 −2.3 −20 446
001002 45 951 12 604 8 854 0 0 0 0 0.00 0.00 0.0 0
001003 53 142 18 510 17 419 0 0 0 0 0.00 0.00 0.0 0
001004 14 302 4 350 2 945 0 0 0 0 0.00 0.00 0.0 0
001005 100 150 35 777 23 334 7 944 3 104 4 840 11 048 23.38 9.13 14.2 −6 208
001006 40 480 13 230 8 716 0 0 0 0 0.00 0.00 0.0 0
001007 48 462 15 781 11 750 0 0 0 0 0.00 0.00 0.0 0
001008 7 164 2 114 1 748 0 0 0 0 0.00 0.00 0.0 0
001009 20 048 5 254 3 186 0 0 0 0 0.00 0.00 0.0 0
001010 18 282 4 668 3 181 0 0 0 0 0.00 0.00 0.0 0
001011 36 822 12 363 7 383 3 318 1 837 1 481 5 155 26.98 14.94 12.0 −3 674
002001 460 793 197 111 189 915 0 0 0 0 0.00 0.00 0.0 0
002002 932 001 384 781 364 575 0 0 0 0 0.00 0.00 0.0 0
002003 95 638 39 811 36 014 761 1 072 −311 1 833 2.25 3.17 −0.9 −2 144
002004 1 543 644 650 723 593 340 4 639 5 003 −364 9 642 0.85 0.91 −0.1 −10 006
002005 91 309 38 424 30 726 4 322 3 647 675 7 969 13.33 11.24 2.1 −7 294
003001 70 358 28 691 27 645 0 0 0 0 0.00 0.00 0.0 0
003002 58 624 24 829 24 457 0 0 0 0 0.00 0.00 0.0 0
003003 249 303 109 569 107 221 0 0 0 0 0.00 0.00 0.0 0
003008 238 498 116 768 111 494 0 0 0 0 0.00 0.00 0.0 0
Fuente: Estimaciones del CONAPO.

Migración intermunicipal

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 12 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
      select(CVE_ENT, NOM_ENT, MUN, CVE_MUN, NOM_MUN, ENT_PAIS_TRAB, MUN_TRAB, CVE_MUN_TRABAJO, 
              EDAD, CONACT, CVE_ZM, NOM_ZM, CVE_ZM_TRABAJO, ZM_TRABAJO, FACTOR, ESTRATO, UPM) %>%
        # Se genera una indicadora de zm 
        mutate(I_ZM_2010 = ifelse(is.na(.$CVE_ZM), '0', '1'),
               I_TRAB_ZM_2010 = ifelse(is.na(.$CVE_ZM_TRABAJO), '0', '1')) %>%
        # Se clasifican a los migrantes internos 
        mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_TRABAJO ~ 'Pertenecen a la Zona Metropolitana', #Trabajan en el mismo municipio
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM == .$CVE_ZM_TRABAJO ~ "Pertenecen a la Zona Metropolitana", #Trabajan en otro municipio dentro de la misma zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM != .$CVE_ZM_TRABAJO ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio pero de otra zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio que no pertenece a la zona metropolitana pero viven en una ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Entran a trabajar a la zona metropolitana pero no pertecen a la ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Trabajan en otro municipio que no es ZM y no residen en una ZM
                                )) %>%
         filter((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
          filter(CVE_MUN_TRABAJO %in% municipios & .$I_ZM %in% "No pertenecen a la Zona Metropolitana") %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_intermunicipal.RDS"))

Matrices

Se genera una matriz cruzada de la movilidad laboral a nivel municipal, utilizando la función svytable de la paquetería survey.

MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_intermunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_TRABAJO + CVE_MUN, design = MC)

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_TRABAJO, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 17, 22)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intermunicipal 2010.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intermunicipal 2010.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intermunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intermunicipal 2010.xlsx"), overwrite = TRUE)

Matriz de movilidad laboral a nivel municipal, 2010.

Matriz de movilidad laboral por zonas metropolitanas
Nivel intermunicipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 0 279 562 4 0 406 449 0 62 97 0 0 0 0 33 0 0 0 0 0 0 0 18 0 0 0 0 0 0
001002 798 0 0 0 64 8 74 0 138 27 218 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 204 0 0 0 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 220 8 26 0 42 10 201 0 17 0 154 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 0 0 265 0 0 103 102 44 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 989 56 24 24 358 0 294 128 226 38 1243 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 630 54 27 142 194 194 0 40 162 0 543 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 144 0 2 6 14 50 32 0 6 6 22 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 580 33 23 33 92 76 682 5 0 0 328 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 755 66 8 0 8 0 8 0 8 0 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 0 40 8 0 0 488 142 24 66 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 0 0 0 0 0 0 0 0 0 0 0 0 246 298 655 264 0 53 28 0 0 0 0 0 0 0 0 0 0
002002 0 0 0 0 0 0 0 0 0 0 0 184 0 155 436 54 0 0 0 0 0 0 0 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 57 84 0 0 0 13 0 7 0 0 0 0 0 0 0 0 0 0
002004 0 0 0 0 0 0 0 0 0 0 0 1695 1252 0 0 0 0 169 311 28 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 231 45 0 0 0 0 10 16 0 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0 0 54 177 66 102 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 30 0 0 0 0 15 0 15 0 0 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 0 14 0 19 0 141 131 0 738 82 0 0 0 0 0 0 0 0
003008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 70 0 5 0 416 0 140 0 0 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 7 7 7 7 0 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 220 258 262 176 110 0 88
004002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 82 0 1347 470 11 39 0 141
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 72 0
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 154 606 0 0 0 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 146 205 211 116 0 50 5 140
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 21 212 111 27 0 0 7 14
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 52 0 0 0 0 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 132 28 14 49 3 3 0
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 33 111 0 0 0 11 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intermunicipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de las Zonas Metropolitanas
NOM_ZM <- stringr::str_wrap(nom_zm, 100)

## Tomamos las Zonas Metropolitanas con más de 3 municipios que tienen flujos migratorios 
#### Con filtro (CF)
ZM_CF <- ZM_2010 %>%
          group_by(CVE_ZM) %>%
           summarise(Count = n()) %>%
            filter(Count >= 0) %>%
             pull(CVE_ZM)

NOM_ZM_CF <- ZM_2010 %>%
              filter(CVE_ZM %in% ZM_CF) %>%
               distinct(CVE_ZM, NOM_ZM)

ZM <- lapply(1:length(ZM_CF), function(x){
                    ZM_2010 %>% 
                     select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
                      filter(CVE_ZM %in% ZM_CF[x])  %>% 
                       mutate(NOM_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>% 
                        pull(NOM_MUN)
})

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)  

################################## Filtro ######################################
#p <- data.frame(ZM = ZM_CF,
 #               filtro_municipio = tabla_municipios,
  #              filtro_estado = tabla_estados)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel intermunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

################################################################################
tabla1 <- migration_flows_metropolitan(tabla = Migrantes, 
                                       filtro_zm = ZM, 
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       Emigrantes = Emigrantes, 
                                       Inmigrantes = Inmigrantes, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group = "Otros estados")

## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:length(ZM_CF), function(i){
#                                           tabla1[[i]] %>%
#                                            as.data.frame() %>%
#                                             adorn_totals(c("row", "col"), 
#                                                           fill = "-", 
#                                                            na.rm = TRUE, 
#                                                             ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                              select(`Otros estados`) %>%
#                                               slice(nrow(.)) %>%
#                                                mutate(`Otros estados` = .$`Otros estados`/10) %>%
#                                                 pull(`Otros estados`)
#})

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:length(ZM_CF), function(i){
#                              p <- tabla1[[i]] %>%
#                                    as.data.frame() %>%
#                                     select(-c(`Otros estados`)) %>%
#                                      slice(-nrow(.))
#                              if(sum(p) == 0) {
#                                return(0)
#                              } else {
#                                p %>% 
#                                 adorn_totals(c("row", "col"), 
#                                                              fill = "-", 
#                                                               na.rm = TRUE, 
#                                                                ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                                  slice(nrow(.)) %>%
#                                                   mutate(Total = .$Total/100) %>%
#                                                    pull(Total)
#                              }
                                           #})

## Se guardan las matrices de movilidad laboral para analizarlos después. 
wb <- createWorkbook()
for(i in 1:length(ZM)){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(ZM_CF[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab a nivel intermunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Tabla MTrab a nivel intermunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Tabla MTrab a nivel intermunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Salen por trabajo", 
                        Emigrantes = "Entran por trabajo")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Salen por trabajo", 
                                  Emigrantes = "%Entran por trabajo") 

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:length(ZM)){
     addWorksheet(wb, paste(ZM_CF[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab a nivel intermunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Tabla MTrab a nivel intermunicipal.RDS"))

# Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B")

tabla2 <- color_chord_diagram(tabla1, paleta)
file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab desagregado por ZM  (Intermunicipal).pdf"
 
## Gráficos a nivel intermunicipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#170A3A",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text = c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Etiquetas ZM a nivel intermunicipal.pdf"

## Etiquetas a nivel intermunicipal
labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = paste(NOM_ZM_CF[,1], NOM_ZM_CF[,2]))

Gráfico Sankey

## Tomamos las Zonas Metropolitanas con más de 3 municipios con flujos migratorios 
ZM_CF <- ZM_2010 %>%
          group_by(CVE_ZM) %>%
           summarise(Count = n()) %>%
            filter(Count > 2) %>%
             pull(CVE_ZM)

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character)
             
tabla1 <- lapply(1:length(ZM_CF), function(x){
                                   ZM <- ZM_2010 %>%
                                          select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
                                           filter(CVE_ZM %in% ZM_CF[x])  %>%
                                            mutate(NOM_MUN = paste(.$CVE_MUN, .$NOM_MUN)) %>%
                                             pull(NOM_MUN)
                                    tabla %>%
                                     mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% ZM | .$cn %in% ZM), value, 0)) %>%
                                      mutate(rn = case_when(.$rn %in% ZM ~ .$rn,
                                                            .$rn %nin% ZM ~ paste0(nom_estados[as.numeric(substr(.$rn, 1, 3))])),
                                             cn = case_when(.$cn %in% ZM ~ .$cn,
                                                            .$cn %nin% ZM ~ paste0(nom_estados[as.numeric(substr(.$cn, 1, 3))]))) %>%
                                       filter(value > 0) 
  }
) 
p <- lapply(1:length(ZM_CF), function(x){
             tabla1[[x]] %>% 
               ggplot(aes(axis1 = rn, 
                           axis2 = cn, 
                            y = value),  # c("value", "freq", "tasa")
                       reverse = FALSE, 
                        na.rm = TRUE) +
                geom_alluvium(aes(fill = rn),
                               curve_type = "quintic", 
                                color = "transparent", 
                                 alpha = 0.85, 
                                  lwd = 0.001, 
                                   width = 1/5,
                                    reverse = FALSE) +
                  geom_stratum(aes(fill = cn), 
                                color = "white", 
                                 alpha = 0.65,  
                                  lwd = 0.001, 
                                   width = 1/5,
                                    reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3, 
                                      direction = "y", 
                                       nudge_x = -.2,
                                        min.segment.length = unit(1, "lines"),
                                         force = 1,
                                          force_pull = 0,
                                           family = "montserrat",
                                            reverse = FALSE) +
                    geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                     stat = "stratum", 
                                      size = 3,
                                       direction = "y", 
                                        nudge_x = .2, 
                                         force = 1,
                                          force_pull = 0,
                                           family = "montserrat",
                                            reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 1.5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 9, family = "montserrat"),
                                   legend.position = c(1, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.35)) +
                        scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/GSankey de MTrab desagregado por ZM_Absolutos (Intermunicipal).pdf")
ggexport(list = p, width = 14, height = 10, dpi = 400, filename = path)

ZMVM

ChordDiagram
ChorDiagram sin grupos
load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intermunicipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

## Se toma como referencia a la Zona Metropolitana del Valle de México
ZM <- ZM_2010 %>% 
       select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
        filter(CVE_ZM %in% "13.01")  %>% 
         mutate(NOM_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>% 
          pull(NOM_MUN)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)       

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             filter(rn %in% ZM) %>%
              mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
               filter(value > 2000) %>% 
                pull(rn)

filtro_est <- Inmigrantes %>%
               full_join(., Emigrantes, by = c("rn" = "cn")) %>%
                filter(rn %nin% ZM) %>%
                 mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
                  mutate(rn = substr(.$rn, 1, 3)) %>%
                   group_by(rn) %>%
                    summarise(value = sum(value)) %>%
                     filter(value > 4000) %>%
                      pull(rn)

################################################################################
tabla1 <- migration_flows_metropolitan_city(tabla = Migrantes, 
                                            filtro_zm = ZM, 
                                            filtro_municipios = filtro, 
                                            filtro_estados = filtro_est, 
                                            category_group = estados, 
                                            category_names = nom_estados,
                                            group = "Otros estados") %>%
            dcast(., rn ~ cn, value.var = "value", sum,  na.rm = TRUE) %>%                        
            column_to_rownames(., var = "rn")    
# Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- rev(c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B"))

tabla2 <- color_chord_diagram(tabla1 = as.matrix(tabla1), paleta)

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab de ZMVM (Intermunicipal).pdf"

## Gráficos a nivel intermunicipal ZMVM 
chord_diagram_graph(file = file, 
                    width = 7, 
                    height = 7, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#170A3A",
                    transparency = 0.1,
                    circo.text = 7,
                    circos.axis.text = 5,
                    adj.text = c(-0.1, 0.5),
                    adj.ylim = 0.2,
                    gap.degree = 3, 
                    clock.wise = FALSE,
                    track.margin = c(-0.2, 0.2),
                    margin = rep(1.5, 4))
ChordDiagram con grupos
load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intermunicipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

## Se toma como referencia a la Zona Metropolitana del Valle de México
ZM <- ZM_2010 %>% 
       select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
        filter(CVE_ZM %in% "13.01")  %>% 
         mutate(NOM_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 50)) %>% 
          pull(NOM_MUN)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)      

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             filter(rn %in% ZM) %>%
              mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
               filter(value > 2000) %>% 
                pull(rn)

filtro_est <- Inmigrantes %>%
               full_join(., Emigrantes, by = c("rn" = "cn")) %>%
                filter(rn %nin% ZM) %>%
                 mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
                  mutate(rn = substr(.$rn, 1, 3)) %>%
                   group_by(rn) %>%
                    summarise(value = sum(value)) %>%
                     filter(value > 4000) %>%
                      pull(rn)

################################################################################
tabla <- migration_flows_metropolitan_city(tabla = Migrantes, 
                                           filtro_zm = ZM, 
                                           filtro_municipios = filtro, 
                                           filtro_estados = filtro_est, 
                                           category_group = estados, 
                                           category_names = nom_estados,
                                           group = "Otros estados")  

tabla1 <- tabla %>%
           dcast(., rn ~ cn, value.var = "value", sum,  na.rm = TRUE) %>%
            column_to_rownames(., var = "rn") 

# Grupo 1
grupo1 <- tabla %>%
           filter(substr(.$rn, 1, 2) == "09") %>%
            pull(rn) %>%
             unique()
# Grupo 2
grupo2 <- tabla %>%
           filter(substr(.$rn, 1, 2) == "15") %>%
            pull(rn) %>%
             unique()
# Grupo 3
grupo3 <- tabla %>%
           filter(substr(.$rn, 1, 2) == "13") %>%
            pull(rn) %>%
             unique()
# Grupo 4
grupo4 <- tabla %>%
           filter(substr(.$rn, 1, 2) %nin% c("09", "15", "13")) %>%
            pull(rn) %>%
             unique()

## Se guardan las matrices de movilidad laboral para analizarlos después. 
tabla <- tabla1 %>%
          as.data.frame() %>%
           adorn_totals(c("row", "col"),  
                         fill = "-", 
                         na.rm = TRUE, 
          ,,,,contains(colnames(tabla1)))

wb <- createWorkbook()
addWorksheet(wb, "ZMVM")
writeData(wb, 1, tabla, colNames = TRUE, rowNames = TRUE)
saveWorkbook(wb, 
              file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab de ZMVM a nivel intermunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
# Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- rev(c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B"))

tabla2 <- color_chord_diagram(tabla1 = as.matrix(tabla1), paleta)

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab de ZMVM_grupos (Intermunicipal).pdf"

## Gráficos a nivel intermunicipal ZMVM 
chord_diagram_graph_zmvm(file = file, 
                         width = 10, 
                         height = 10, 
                         family = "Montserrat Medium", 
                         paleta = paleta, 
                         tabla1 = as.matrix(tabla1), 
                         color_labels = "#170A3A",
                         transparency = 0.1,
                         circo.text = 9,
                         circos.axis.text = 7,
                         adj.text = c(-0.01, 0.5),
                         adj.ylim = 1,
                         gap.degree = 3, 
                         clock.wise = TRUE,
                         track.margin = c(-0.2, 0.2),
                         margin = rep(0, 4), 
                         group1 = grupo1, 
                         group1.text = "Ciudad de México",
                         group1.col = 1, 
                         group2 = grupo2, 
                         group2.text = "México",
                         group2.col = 15, 
                         group3 = grupo3, 
                         group3.text = "Hidalgo",
                         group3.col = 20, 
                         group4 = grupo4, 
                         group4.text = "Otro municipios",
                         group4.col = 30)

Etiquetas

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Etiquetas ZMVM a nivel intermunicipal.pdf"

## Etiquetas a nivel intermunicipal ZMVM 
labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 9, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = "ZM del Valle de México")
Gráfico Sankey

Zona Metropolitana del Valle de México (ZMVM)

load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intermunicipal 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN, by = c("CVE_MUN")) %>%
                mutate(CVE_MUN = paste(.$CVE_MUN, .$NOM_MUN)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN, by = c("CVE_MUN")) %>%
                mutate(CVE_MUN = paste(.$CVE_MUN, .$NOM_MUN)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

## Se toma como referencia a la Zona Metropolitana del Valle de México
ZM <- ZM_2010 %>% 
       select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
        filter(CVE_ZM %in% "13.01")  %>% 
         mutate(NOM_MUN = paste(.$CVE_MUN, .$NOM_MUN)) %>% 
          pull(NOM_MUN)

##########################################################################################
######################################## Filtro ##########################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)  

######################################## Filtro ##########################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = Inmigrantes + Emigrantes) %>%
              filter(rn %in% ZM) %>%
               filter(value > 30000) %>% 
                pull(rn)
#########################################################################################
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>% 
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% ZM | .$cn %in% ZM), value, 0)) %>% 
               mutate(rn = case_when(.$rn %in% ZM & .$rn %in% filtro  ~ .$rn,
                                     .$rn %in% ZM & .$rn %nin% filtro ~ str_wrap(paste(estados[as.numeric(substr(.$rn, 1, 3))], "ZMVM"), 20),
                                     .$rn %nin% ZM & .$rn %nin% filtro ~ str_wrap(paste0(nom_estados[as.numeric(substr(.$rn, 1, 3))]), 20)),
                       
                      cn = case_when(.$cn %in% ZM & .$cn %in% filtro  ~ .$cn,
                                     .$cn %in% ZM & .$cn %nin% filtro ~ str_wrap(paste(estados[as.numeric(substr(.$cn, 1, 3))], "ZMVM"), 20),
                                     .$cn %nin% ZM & .$cn %nin% filtro ~ str_wrap(paste0(nom_estados[as.numeric(substr(.$cn, 1, 3))]), 20))) %>%
                filter(value > 0)  

p <- tabla %>% 
      ggplot(aes(axis1 = rn, 
                  axis2 = cn, 
                   y = value),  # c("value", "freq", "tasa")
              reverse = FALSE, 
               na.rm = TRUE) +
       geom_alluvium(aes(fill = rn),
                      curve_type = "quintic", 
                       color = "transparent", 
                        alpha = 0.85,  
                         lwd = 0.001, 
                          width = 1/5,
                           reverse = FALSE) +
         geom_stratum(aes(fill = cn), 
                       color = "white", 
                        alpha = 0.65,  
                         lwd = 0.001, 
                          width = 1/5, 
                           reverse = FALSE) +
           geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                               fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                            stat = "stratum", 
                             size = 3, 
                              direction = "y", 
                               nudge_x = -.23,
                                min.segment.length = unit(1, "lines"),
                                 force = 1,
                                  force_pull = 0,
                                   family = "montserrat",
                                    reverse = FALSE) +
            geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                             stat = "stratum", 
                              size = 3,
                               direction = "y", 
                                nudge_x = .23, 
                                 force = 1,
                                  force_pull = 0,
                                   family = "montserrat",
                                    reverse = FALSE) +
             theme_void() +  
              theme(plot.margin = margin(t = 1, r = 4, b = 1, l = 0, "cm"),
                     text = element_text(family = "montserrat"),
                      axis.text = element_blank(),
                       axis.title = element_blank(),
                        strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                         legend.key.size = unit(0.5, "cm"),
                          legend.text = element_text(size = 9, family = "montserrat"),
                           legend.position = c(0.999, .5)) + 
               scale_x_discrete(expand = c(-0.1, 0.5)) +
                scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                 guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                  labs(fill = "", 
                       color = "")

path = paste0(here::here(), "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/GSankey de MTrab de la ZMVM  (Intermunicipal).pdf")
ggexport(p, width = 20, height = 12, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
##################### Población de 12 años y más ###############################
Pob.ocupada <- mydata %>%
                as.data.frame() %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
                   group_by(CVE_MUN) %>%
                    summarise(Pob.ocupada = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intermunicipal 2010.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################

## Población que sale de su entidad de residencia y entra a otra demarcación por motivos de trabajo
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_TRABAJO) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para trabajar
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_TRABAJO) %>%
                     group_by(CVE_MUN_TRABAJO) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_TRABAJO") 

tabla <- Pob.Total %>%
          left_join(., Pob.ocupada, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Intermunicipal).xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Intermunicipal).RData"))
Indicadores de movilidad laboral a nivel intermunicipal
Zonas Metropolitanas
CVE_MUN Pob.Total Pob.ocupada Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 793 997 322 616 0 5 593 11 087 −5 494 16 680 2.00 3.97 −2.0 −22 174
001002 45 951 12 604 0 1 960 714 1 246 2 674 13.39 4.88 8.5 −1 428
001003 53 142 18 510 0 370 1 034 −664 1 404 2.07 5.77 −3.7 −2 068
001004 14 302 4 350 0 821 281 540 1 102 17.61 6.03 11.6 −562
001005 100 150 35 777 0 856 986 −130 1 842 2.52 2.90 −0.4 −1 972
001006 40 480 13 230 0 3 504 1 373 2 131 4 877 26.10 10.23 15.9 −2 746
001007 48 462 15 781 0 2 498 2 204 294 4 702 15.55 13.72 1.8 −4 408
001008 7 164 2 114 0 288 258 30 546 12.42 11.12 1.3 −516
001009 20 048 5 254 0 1 930 711 1 219 2 641 30.51 11.24 19.3 −1 422
001010 18 282 4 668 0 937 464 473 1 401 16.33 8.09 8.2 −928
001011 36 822 12 363 0 878 2 890 −2 012 3 768 7.14 23.50 −16.4 −5 780
002001 460 793 197 111 0 1 827 2 717 −890 4 544 1.11 1.65 −0.5 −5 434
002002 932 001 384 781 0 1 835 3 145 −1 310 4 980 0.56 0.96 −0.4 −6 290
002003 95 638 39 811 0 243 495 −252 738 0.72 1.46 −0.7 −990
002004 1 543 644 650 723 0 3 997 3 607 390 7 604 0.73 0.66 0.1 −7 214
002005 91 309 38 424 0 375 362 13 737 1.16 1.12 0.0 −724
003001 70 358 28 691 0 417 215 202 632 1.68 0.87 0.8 −430
003002 58 624 24 829 0 75 472 −397 547 0.36 2.26 −1.9 −944
003003 249 303 109 569 0 1 199 1 599 −400 2 798 1.34 1.78 −0.4 −3 198
003008 238 498 116 768 0 973 1 910 −937 2 883 1.10 2.15 −1.1 −3 820
Fuente: Estimaciones del CONAPO.

Movilidad metropolitana

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 12 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
      select(CVE_ENT, NOM_ENT, MUN, CVE_MUN, NOM_MUN, ENT_PAIS_TRAB, MUN_TRAB, CVE_MUN_TRABAJO, 
              EDAD, CONACT, CVE_ZM, NOM_ZM, CVE_ZM_TRABAJO, ZM_TRABAJO, FACTOR, ESTRATO, UPM) %>%
        # Se genera una indicadora de zm 
        mutate(I_ZM_2010 = ifelse(is.na(.$CVE_ZM), '0', '1'),
               I_TRAB_ZM_2010 = ifelse(is.na(.$CVE_ZM_TRABAJO), '0', '1')) %>%
        # Se clasifican a los migrantes internos 
        mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_TRABAJO ~ 'Pertenecen a la Zona Metropolitana', #Trabajan en el mismo municipio
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM == .$CVE_ZM_TRABAJO ~ "Pertenecen a la Zona Metropolitana", #Trabajan en otro municipio dentro de la misma zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM != .$CVE_ZM_TRABAJO ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio pero de otra zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio que no pertenece a la zona metropolitana pero viven en una ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Entran a trabajar a la zona metropolitana pero no pertecen a la ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Trabajan en otro municipio que no es ZM y no residen en una ZM
                                )) %>%
         filter((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
          filter(CVE_MUN_TRABAJO %in% municipios) %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_metropolitana.RDS"))

Matrices

Se genera una matriz cruzada de la movilidad laboral a nivel municipal, utilizando la función svytable de la paquetería survey.

MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_metropolitana.RDS"))

Migrantes <- svytable(~CVE_ZM_TRABAJO + CVE_ZM, design = MC)

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_ZM, CVE_ZM_TRABAJO, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_ZM" = "row_labels") %>% 
                  arrange(CVE_ZM) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_ZM = substr(.$CVE_ZM, 8, 12)) %>% 
              pull(CVE_ZM)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_ZM" = ".") %>%
                mutate(`CVE_ZM` = substr(.$CVE_ZM, 16, 20)) %>%
                 pull(CVE_ZM)

# Se elimina la variable CVE_ZM
Migrantes <- Migrantes %>%
              select(-CVE_ZM)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de Movilidad laboral a nivel metropolitano 2010.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de Movilidad laboral a nivel metropolitano 2010.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Metropolitano")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_ZM"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de Movilidad laboral a nivel metropolitano 2010.xlsx"), overwrite = TRUE)

Matriz de Movilidad laboral a nivel municipal, 2010.

Matriz de Movilidad laboral por zonas metropolitanas
Zonas metropolitanas
CVE_ZM 01.01 02.01 03.01 04.01 05.01 06.01 07.01 08.01 09.01 10.01 11.01 12.01 13.01 14.01 15.01 16.01 17.01 18.01 19.01 20.01 21.01 22.01 23.01 24.01 25.01 26.01 27.01 28.01 29.01
01.01 352669 33 0 4 0 0 0 0 0 0 0 0 178 238 0 0 0 0 0 0 310 0 0 32 0 0 0 29 0
02.01 0 669802 1381 27 0 0 0 0 0 0 0 23 13 0 0 0 0 0 0 0 92 0 0 0 3 0 0 0 0
03.01 0 645 364575 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
04.01 87 0 0 404271 728 82 90 0 0 0 73 143 23 7 0 0 10 0 0 0 180 0 0 7 24 0 0 0 0
05.01 144 0 0 217 318719 179 138 16 0 0 0 34 0 16 0 0 0 26 0 0 76 0 0 62 0 0 0 0 0
06.01 6 0 0 0 448 115155 98 0 0 0 0 17 6 38 0 8 0 0 0 0 13 0 0 2 0 0 0 0 0
07.01 38 0 0 26 86 28 65962 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 84
08.01 62 18 0 0 0 0 0 137966 3093 0 15 77 122 12 0 0 0 0 0 0 364 26 0 0 20 0 4 0 0
09.01 6 0 0 0 9 0 0 541 57767 0 0 7 19 0 0 0 0 0 0 0 68 8 0 0 85 0 0 0 0
10.01 0 0 0 0 0 0 0 44 0 262806 0 0 32 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0
11.01 0 9 0 36 0 0 0 0 0 0 493888 279 0 0 0 0 0 0 0 0 63 0 0 0 0 0 0 0 0
12.01 0 0 0 59 0 0 0 0 0 35 347 341409 125 0 0 0 0 0 0 0 212 0 0 0 0 0 0 0 0
13.01 164 192 111 247 112 0 50 125 0 95 308 407 7506232 431 0 34 430 4812 517 1961 2853 269 3 30096 452 44 0 2576 2106
14.01 168 34 0 0 8 0 0 0 0 0 100 6 63 638234 2482 45 1 0 0 0 347 36 0 37 230 96 48 0 0
15.01 8 0 0 0 0 12 0 0 0 0 0 0 0 3116 70181 0 0 0 0 0 31 0 0 0 9 0 0 0 0
16.01 0 0 0 0 0 0 0 0 0 0 0 0 14 57 0 42684 27 0 0 0 14 0 0 0 57 15 0 0 0
17.01 0 0 0 0 0 0 0 0 0 0 0 0 164 0 0 0 336818 0 0 0 80 0 0 30 0 0 0 59 0
18.01 7 0 0 25 0 0 0 11 0 0 0 45 5630 0 0 0 4 194257 1089 842 91 4 0 225 23 0 0 22 0
19.01 0 10 0 0 0 0 0 12 0 0 0 0 539 18 0 0 0 1205 85738 87 37 0 0 61 0 0 11 21 0
20.01 0 0 0 0 0 12 1 0 0 0 0 0 1453 0 0 0 0 513 6 68431 46 0 0 47 38 0 0 0 0
21.01 570 357 43 0 0 0 0 657 89 0 0 39 447 101 52 22 0 0 0 0 1842758 812 956 198 51 24 0 0 0
22.01 39 0 0 0 0 0 0 0 0 0 0 33 40 0 0 0 0 0 0 0 448 159054 0 13 0 0 0 0 0
23.01 0 0 16 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 2873 43 44944 0 13 21 0 0 0
24.01 98 3 28 26 0 0 0 12 0 60 0 30 23358 100 0 0 31 91 3 10 364 3 0 677730 92 0 0 613 38
25.01 0 0 0 0 0 0 0 1 0 0 0 0 509 0 0 214 0 0 0 0 372 0 0 142 315812 215 136 98 0
26.01 0 0 0 0 0 0 0 0 0 0 0 7 16 13 0 0 0 0 0 0 138 8 0 13 59 91172 105 0 0
27.01 0 0 0 2 0 0 0 0 0 0 0 0 0 128 0 0 0 0 0 0 10 9 0 0 43 124 72363 0 0
28.01 0 0 0 0 10 0 0 7 0 0 0 0 2643 1 0 0 135 0 0 0 6 0 0 297 23 0 0 362996 4162
29.01 0 0 0 0 0 0 0 0 0 0 20 210 1634 0 0 0 0 0 0 0 31 14 0 139 16 0 0 6086 152117
30.01 0 61 0 14 0 0 0 0 0 0 0 0 121 0 0 0 0 0 0 0 401 780 0 0 0 0 0 67 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por Zonas Metropolitanas

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de Movilidad laboral a nivel metropolitano 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_ZM" = ".") %>%
               left_join(., ZM_2010 %>% select(CVE_ZM, NOM_ZM) %>% distinct(CVE_ZM, NOM_ZM), by = c("CVE_ZM")) %>%
                mutate(CVE_ZM = stringr::str_wrap(paste(.$CVE_ZM, .$NOM_ZM), 100)) %>%
                 pull(CVE_ZM)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_ZM" = ".") %>%
               left_join(., ZM_2010 %>% select(CVE_ZM, NOM_ZM) %>% distinct(CVE_ZM, NOM_ZM), by = c("CVE_ZM")) %>%
                mutate(CVE_ZM = stringr::str_wrap(paste(.$CVE_ZM, .$NOM_ZM), 100)) %>%
                 pull(CVE_ZM)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de las Zonas Metropolitanas
NOM_ZM <- stringr::str_wrap(nom_zm, 100)

################################################################################
tabla1 <- lapply(1, function(x){
                         Migrantes %>%
                          as.data.frame() %>%
                           tibble::rownames_to_column(var = "rn") %>% 
                            melt(., id.vars = "rn", variable.name = "cn") %>%
                             mutate_if(is.factor, as.character) %>%
                              mutate(value = ifelse((.$rn != .$cn), value, 0)) %>% 
                               filter(value > 0) %>%
                                dcast(., rn ~ cn, value.var = "value", sum,  na.rm = TRUE) %>%
                                 column_to_rownames(., var = "rn") 
}
)
# Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B")

tabla2 <- color_chord_diagram(tabla1, paleta)
file =  "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MTrab desagregado por ZM (metropolitano).pdf"

## Gráficos a nivel metropolitano
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#170A3A",
                    transparency = 0,
                    circo.text = 7,
                    circos.axis.text = 5,
                    adj.text = c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))
Gráficos por Zonas Metropolitanas

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de Movilidad laboral a nivel metropolitano 2010.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_ZM" = ".") %>%
               left_join(., ZM_2010 %>% select(CVE_ZM, NOM_ZM) %>% distinct(CVE_ZM, NOM_ZM), by = c("CVE_ZM")) %>%
                mutate(CVE_ZM = stringr::str_wrap(paste(.$CVE_ZM, .$NOM_ZM), 100)) %>%
                 pull(CVE_ZM)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_ZM" = ".") %>%
               left_join(., ZM_2010 %>% select(CVE_ZM, NOM_ZM) %>% distinct(CVE_ZM, NOM_ZM), by = c("CVE_ZM")) %>%
                mutate(CVE_ZM = stringr::str_wrap(paste(.$CVE_ZM, .$NOM_ZM), 100)) %>%
                 pull(CVE_ZM)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de las Zonas Metropolitanas
ZM <- ZM_2010 %>% 
       select(CVE_ZM, NOM_ZM) %>% 
        distinct(CVE_ZM, NOM_ZM) %>%
         mutate(CVE_ZM = stringr::str_wrap(paste(.$CVE_ZM, .$NOM_ZM), 100)) %>%
          pull(CVE_ZM)

NOM_ZM_CF <- ZM_2010 %>%
              filter(CVE_ZM %in% ZM_CF) %>%
               distinct(CVE_ZM, NOM_ZM)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(ZM = ZM, tabla = Migrantes)

Emigrantes <- Emigrantes_function(ZM = ZM, tabla = Migrantes)  

################################## Filtro ######################################
#p <- data.frame(ZM = ZM,
 #               filtro_zm = filtro_mig)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel metropolitano.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Filtro a nivel metropolitano.xlsx"), colNames = TRUE) %>%
               pull(filtro_zm)

################################################################################
tabla1 <- metropolitan_flows(tabla = Migrantes, 
                             filtro_zm = ZM, 
                             filtro_mig = filtro_mig, 
                             Emigrantes = Emigrantes, 
                             Inmigrantes = Inmigrantes, 
                             group = "Otras zonas metropolitanas")

## Se guardan las matrices de movilidad laboral para analizarlos después. 
wb <- createWorkbook()
for(i in 1:length(ZM)){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, zm[i])
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab a nivel metropolitano_Reduccion.xlsx"), 
               overwrite = TRUE)
}
saveRDS(tabla1, paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab a nivel metropolitano.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab a nivel metropolitano.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Salen por trabajo", 
                        Emigrantes = "Entran por trabajo")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Salen por trabajo", 
                                  Emigrantes = "%Entran por trabajo") 

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:length(ZM)){
     addWorksheet(wb, paste(ZM_CF[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab a nivel metropolitano_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}
tabla1 <- readRDS(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz MTrab a nivel metropolitano.RDS"))

# Paleta de colores
#paleta <- colorRampPalette(pals::ocean.matter(100))(50)
paleta <- c("#170A3A",  "#7D1D6B", "#871D62","#952664", "#AE2A5E", "#CA3F56", "#DB5854", "#E45B2F", "#E86328", "#ED8861", "#F4A472", "#F4CA72", "#E8B94B")

tabla2 <- color_chord_diagram(tabla1, paleta)
file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/ChordDiagram de MEst desagregado por ZM (metropolitano)_individual.pdf"
 
## Gráficos a nivel metropolitano
chord_diagram_graph(file = file, 
                    width = 8, 
                    height = 8, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#170A3A",
                    transparency = 0,
                    circo.text = 7,
                    circos.axis.text = 5,
                    adj.text = c(-0.15, 0.5),
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE, 
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Etiquetas ZM a nivel metropolitano.pdf"

## Etiquetas a nivel zona metropolitana
labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1,
                     labels = paste(NOM_ZM_CF[,1], NOM_ZM_CF[,2]))

Indicadores

Se realizan cálculos generales de migración:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_ZM) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población ocupada #######################################
Pob.ocupada <- mydata %>%
                as.data.frame() %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
                   group_by(CVE_ZM) %>%
                    summarise(Pob.ocupada = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel municipal 2010.RData"))

ZM <- lapply(1:length(zm), function(x){
                 ZM_2010 %>% 
                  select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
                   filter(CVE_ZM %in% zm[x])  %>% 
                    pull(CVE_MUN)
})

Residentes <- lapply(1:length(zm), function(x){
                                     Migrantes %>% 
                                      as.data.frame() %>%
                                       tibble::rownames_to_column(var = "CVE_MUN") %>%
                                        melt(., id.vars =  "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                                         mutate_at(vars(3), as.numeric) %>%
                                          filter(CVE_MUN == CVE_MUN_TRABAJO)  %>%
                                           filter(CVE_MUN %in% ZM[[x]]) %>%
                                            summarize(Residentes =  sum(value, na.rm = TRUE)) %>% 
                                             mutate(CVE_ZM = !!paste0(zm[x])) 
})

Residentes <- do.call(rbind.data.frame, Residentes)

################################################################################
############################### Inmigrantes ####################################

## Población que sale de su entidad de residencia y entra a otra demarcación por motivos de trabajo
Inmigrantes <- lapply(1:length(zm), function(x){
                                     Migrantes %>% 
                                      as.data.frame() %>%
                                       tibble::rownames_to_column(var = "CVE_MUN") %>%
                                        melt(., id.vars =  "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                                         mutate_at(vars(3), as.numeric) %>%
                                          filter(CVE_MUN != CVE_MUN_TRABAJO)  %>%
                                           filter(CVE_MUN %in% ZM[[x]]) %>%
                                            summarize(Inmigrantes =  sum(value, na.rm = TRUE)) %>% 
                                             mutate(CVE_ZM = !!paste0(zm[x])) 
})

Inmigrantes <- do.call(rbind.data.frame, Inmigrantes)

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para trabajar
Emigrantes <- lapply(1:length(zm), function(x){
                                     Migrantes %>% 
                                      t() %>%
                                       as.data.frame() %>%
                                        tibble::rownames_to_column(var = "CVE_MUN") %>%
                                         melt(., id.vars =  "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                                          mutate_at(vars(3), as.numeric) %>%
                                           filter(CVE_MUN != CVE_MUN_TRABAJO)  %>%
                                           filter(CVE_MUN %in% ZM[[x]]) %>%
                                            summarize(Emigrantes =  sum(value, na.rm = TRUE)) %>% 
                                             mutate(CVE_ZM = !!paste0(zm[x])) 
})

Emigrantes <- do.call(rbind.data.frame, Emigrantes)

tabla <- Pob.Total %>%
          left_join(., Pob.ocupada, by = c("CVE_ZM")) %>%
          left_join(., Residentes, by = c("CVE_ZM")) %>%
          left_join(., Inmigrantes, by = c("CVE_ZM")) %>%
          left_join(., Emigrantes, by = c("CVE_ZM")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Metropolitano) 2010.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Metropolitano) 2010.RData"))
Indicadores de movilidad laboral (Nivel metropolitano)
Zonas Metropolitanas
CVE_ZM Pob.Total Pob.ocupada Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
01.01 930 969 370 756 337 505 22 491 30 127 −7 636 52 618 6.91 9.26 −2.3 −60 254
02.01 1 730 591 728 958 660 080 14 337 14 186 151 28 523 2.33 2.31 0.0 −28 372
03.01 932 001 384 781 364 575 1 835 3 145 −1 310 4 980 0.56 0.96 −0.4 −6 290
04.01 1 214 931 434 981 365 271 48 465 47 966 499 96 431 11.75 11.63 0.1 −95 932
05.01 820 838 331 678 286 111 37 604 37 552 52 75 156 13.05 13.03 0.0 −75 104
06.01 316 334 120 770 100 883 16 728 19 422 −2 694 36 150 15.31 17.77 −2.5 −38 844
07.01 178 886 70 402 62 663 4 682 6 992 −2 310 11 674 7.51 11.22 −3.7 −13 984
08.01 330 435 148 927 103 508 41 537 38 232 3 305 79 769 34.66 31.90 2.8 −76 464
09.01 141 869 61 634 56 761 3 601 5 112 −1 511 8 713 7.08 10.05 −3.0 −10 224
10.01 683 404 280 457 251 508 24 020 28 123 −4 103 52 143 9.97 11.67 −1.7 −56 246
11.01 1 328 246 516 908 493 888 1 514 2 706 −1 192 4 220 0.33 0.59 −0.3 −5 412
12.01 847 021 353 754 339 098 6 774 8 562 −1 788 15 336 2.26 2.85 −0.6 −17 124
13.01 20 014 450 8 415 797 4 646 044 2 974 718 2 957 421 17 297 5 932 139 41.85 41.61 0.2 −5 914 842
14.01 1 604 170 662 785 627 674 20 511 33 736 −13 225 54 247 3.62 5.95 −2.3 −67 472
15.01 183 266 75 414 62 776 11 010 12 600 −1 590 23 610 17.02 19.48 −2.5 −25 200
16.01 110 797 44 487 39 433 4 172 8 173 −4 001 12 345 10.75 21.05 −10.3 −16 346
17.01 859 676 347 862 333 843 7 763 7 636 127 15 399 2.57 2.53 0.0 −15 272
18.01 515 075 218 944 152 870 55 919 60 204 −4 285 116 123 30.47 32.81 −2.3 −120 408
19.01 242 261 95 020 78 074 13 115 13 046 69 26 161 15.55 15.47 0.1 −26 092
20.01 205 954 78 275 60 172 14 844 21 038 −6 194 35 882 20.89 29.61 −8.7 −42 076
Fuente: Estimaciones del CONAPO.

Migración intrametropolitana

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 12 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
      select(CVE_ENT, NOM_ENT, MUN, CVE_MUN, NOM_MUN, ENT_PAIS_TRAB, MUN_TRAB, CVE_MUN_TRABAJO, 
              EDAD, CONACT, CVE_ZM, NOM_ZM, CVE_ZM_TRABAJO, ZM_TRABAJO, FACTOR, ESTRATO, UPM) %>%
        # Se genera una indicadora de zm 
        mutate(I_ZM_2010 = ifelse(is.na(.$CVE_ZM), '0', '1'),
               I_TRAB_ZM_2010 = ifelse(is.na(.$CVE_ZM_TRABAJO), '0', '1')) %>%
        # Se clasifican a los migrantes internos 
        mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_TRABAJO ~ 'Pertenecen a la Zona Metropolitana', #Trabajan en el mismo municipio
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM == .$CVE_ZM_TRABAJO ~ "Pertenecen a la Zona Metropolitana", #Trabajan en otro municipio dentro de la misma zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM != .$CVE_ZM_TRABAJO ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio pero de otra zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio que no pertenece a la zona metropolitana pero viven en una ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Entran a trabajar a la zona metropolitana pero no pertecen a la ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Trabajan en otro municipio que no es ZM y no residen en una ZM
                                )) %>%
         filter((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
          filter(CVE_MUN_TRABAJO %in% municipios & .$I_ZM %in% "Pertenecen a la Zona Metropolitana") %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_intrametropolitana.RDS"))

Indicadores

Se realizan cálculos generales de migración:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_ZM) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población ocupada #######################################
Pob.ocupada <- mydata %>%
                as.data.frame() %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
                   group_by(CVE_ZM) %>%
                    summarise(Pob.ocupada = sum(FACTOR))  

################################################################################
########################### Residentes #########################################
MR <- readRDS(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matrices de Mtrab a nivel intramunicipal por ZM2010.RDS"))

Residentes <- lapply(1:length(zm), function(x){
                                     MR[[x]] %>% 
                                      as.data.frame() %>%
                                       melt(., id.vars =  "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                                        mutate_at(vars(3), as.numeric) %>%
                                         filter(CVE_MUN == CVE_MUN_TRABAJO)  %>%
                                          summarize(Residentes =  sum(value, na.rm = TRUE)) %>% 
                                           mutate(CVE_ZM = !!paste0(zm[x])) 
})

Residentes <- do.call(rbind.data.frame, Residentes)

################################################################################
############################### Inmigrantes ####################################

## Población que sale de su entidad de residencia y entra a otra demarcación por motivos de trabajo
Inmigrantes <- lapply(1:length(zm), function(x){
                                      MR[[x]] %>% 
                                       as.data.frame() %>%
                                        melt(., id.vars =  "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                                         mutate_at(vars(3), as.numeric) %>%
                                          filter(CVE_MUN != CVE_MUN_TRABAJO)  %>%
                                           summarize(Inmigrantes =  sum(value, na.rm = TRUE)) %>% 
                                            mutate(CVE_ZM = !!paste0(zm[x])) 
})

Inmigrantes <- do.call(rbind.data.frame, Inmigrantes)

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para trabajar
Emigrantes <- lapply(1:length(zm), function(x){
                                    MR[[x]] %>% 
                                     tibble::column_to_rownames(var = "CVE_MUN") %>%
                                      t() %>%
                                       as.data.frame() %>%
                                        tibble::rownames_to_column(var = "CVE_MUN") %>%
                                         melt(., id.vars =  "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                                          mutate_at(vars(3), as.numeric) %>%
                                           filter(CVE_MUN != CVE_MUN_TRABAJO)  %>%
                                            summarize(Emigrantes=  sum(value, na.rm = TRUE)) %>% 
                                             mutate(CVE_ZM = !!paste0(zm[x])) 
})

Emigrantes <- do.call(rbind.data.frame, Emigrantes)

tabla <- Pob.Total %>%
          left_join(., Pob.ocupada, by = c("CVE_ZM")) %>%
          left_join(., Residentes, by = c("CVE_ZM")) %>%
          left_join(., Inmigrantes, by = c("CVE_ZM")) %>%
          left_join(., Emigrantes, by = c("CVE_ZM")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Intrametropolitano) 2010.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Intrametropolitano) 2010.RData"))
Indicadores de movilidad laboral (Nivel intrametropolitano)
Zonas Metropolitanas
CVE_ZM Pob.Total Pob.ocupada Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
01.01 930 969 370 756 337 505 15 164 15 164 0 30 328 4.66 4.66 0.0 −30 328
02.01 1 730 591 728 958 660 080 9 722 9 722 0 19 444 1.58 1.58 0.0 −19 444
03.01 932 001 384 781 364 575 0 0 0 0 0.00 0.00 0.0 0
04.01 1 214 931 434 981 365 271 39 000 39 000 0 78 000 9.46 9.46 0.0 −78 000
05.01 820 838 331 678 286 111 32 608 32 608 0 65 216 11.32 11.32 0.0 −65 216
06.01 316 334 120 770 100 883 14 272 14 272 0 28 544 13.06 13.06 0.0 −28 544
07.01 178 886 70 402 62 663 3 299 3 299 0 6 598 5.29 5.29 0.0 −6 598
08.01 330 435 148 927 103 508 34 458 34 458 0 68 916 28.75 28.75 0.0 −68 916
09.01 141 869 61 634 56 761 1 006 1 006 0 2 012 1.98 1.98 0.0 −2 012
10.01 683 404 280 457 251 508 11 298 11 298 0 22 596 4.69 4.69 0.0 −22 596
11.01 1 328 246 516 908 493 888 0 0 0 0 0.00 0.00 0.0 0
12.01 847 021 353 754 339 098 2 311 2 311 0 4 622 0.77 0.77 0.0 −4 622
13.01 20 014 450 8 415 797 4 646 044 2 860 188 2 860 188 0 5 720 376 40.24 40.24 0.0 −5 720 376
14.01 1 604 170 662 785 627 674 10 560 10 560 0 21 120 1.86 1.86 0.0 −21 120
15.01 183 266 75 414 62 776 7 405 7 405 0 14 810 11.45 11.45 0.0 −14 810
16.01 110 797 44 487 39 433 3 251 3 251 0 6 502 8.37 8.37 0.0 −6 502
17.01 859 676 347 862 333 843 2 975 2 975 0 5 950 0.99 0.99 0.0 −5 950
18.01 515 075 218 944 152 870 41 387 41 387 0 82 774 22.55 22.55 0.0 −82 774
19.01 242 261 95 020 78 074 7 664 7 664 0 15 328 9.09 9.09 0.0 −15 328
20.01 205 954 78 275 60 172 8 259 8 259 0 16 518 11.62 11.62 0.0 −16 518
Fuente: Estimaciones del CONAPO.

Migración intermetropolitana

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 12 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
      select(CVE_ENT, NOM_ENT, MUN, CVE_MUN, NOM_MUN, ENT_PAIS_TRAB, MUN_TRAB, CVE_MUN_TRABAJO, 
              EDAD, CONACT, CVE_ZM, NOM_ZM, CVE_ZM_TRABAJO, ZM_TRABAJO, FACTOR, ESTRATO, UPM) %>%
        # Se genera una indicadora de zm 
        mutate(I_ZM_2010 = ifelse(is.na(.$CVE_ZM), '0', '1'),
               I_TRAB_ZM_2010 = ifelse(is.na(.$CVE_ZM_TRABAJO), '0', '1')) %>%
        # Se clasifican a los migrantes internos 
        mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_TRABAJO ~ 'Pertenecen a la Zona Metropolitana', #Trabajan en el mismo municipio
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM == .$CVE_ZM_TRABAJO ~ "Pertenecen a la Zona Metropolitana", #Trabajan en otro municipio dentro de la misma zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '1' & .$CVE_ZM != .$CVE_ZM_TRABAJO ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio pero de otra zona metropolitana
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '1' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Trabajan en otro municipio que no pertenece a la zona metropolitana pero viven en una ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Entran a trabajar a la zona metropolitana pero no pertecen a la ZM
                                .$CVE_MUN != .$CVE_MUN_TRABAJO & .$I_ZM_2010 %in% '0' & .$I_TRAB_ZM_2010 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Trabajan en otro municipio que no es ZM y no residen en una ZM
                                )) %>%
         filter((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
          filter(CVE_MUN_TRABAJO %in% municipios & .$I_ZM %in% "No pertenecen a la Zona Metropolitana") %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/MC_intermetropolitana.RDS"))

Indicadores

Se realizan cálculos generales de migración:

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_ZM) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población ocupada #######################################
Pob.ocupada <- mydata %>%
                as.data.frame() %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset((EDAD >= 12 & EDAD <= 130) & (CONACT >= 10 & CONACT <= 20)) %>%
                   group_by(CVE_ZM) %>%
                    summarise(Pob.ocupada = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Matriz de movilidad laboral a nivel intermunicipal 2010.RData"))

ZM <- lapply(1:length(zm), function(x){
                    ZM_2010 %>% 
                     select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
                      filter(CVE_ZM %in% zm[x])  %>% 
                      pull(CVE_MUN)
})

Residentes <- lapply(1:length(zm), function(x){
                                     Migrantes %>% 
                                      as.data.frame() %>%
                                       tibble::rownames_to_column(var = "CVE_MUN") %>%
                                        melt(., id.vars =  "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                                         mutate_at(vars(3), as.numeric) %>%
                                          filter(CVE_MUN == CVE_MUN_TRABAJO)  %>%
                                           filter(CVE_MUN %in% ZM[[x]]) %>%
                                            summarize(Residentes =  sum(value, na.rm = TRUE)) %>% 
                                             mutate(CVE_ZM = !!paste0(zm[x])) 
})

Residentes <- do.call(rbind.data.frame, Residentes)

################################################################################
############################### Inmigrantes ####################################

## Población que sale de su entidad de residencia y entra a otra demarcación por motivos de trabajo
Inmigrantes <- lapply(1:length(zm), function(x){
                                     Migrantes %>% 
                                      as.data.frame() %>%
                                       tibble::rownames_to_column(var = "CVE_MUN") %>%
                                        melt(., id.vars =  "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                                         mutate_at(vars(3), as.numeric) %>%
                                          filter(CVE_MUN != CVE_MUN_TRABAJO)  %>%
                                           filter(CVE_MUN %in% ZM[[x]]) %>%
                                            summarize(Inmigrantes =  sum(value, na.rm = TRUE)) %>% 
                                             mutate(CVE_ZM = !!paste0(zm[x])) 
})

Inmigrantes <- do.call(rbind.data.frame, Inmigrantes)

################################################################################
############################### Emigrantes #####################################

## Población que entra a la entidad para trabajar
Emigrantes <- lapply(1:length(zm), function(x){
                                     Migrantes %>% 
                                      t() %>%
                                       as.data.frame() %>%
                                        tibble::rownames_to_column(var = "CVE_MUN") %>%
                                         melt(., id.vars =  "CVE_MUN", variable.name = "CVE_MUN_TRABAJO") %>%
                                          mutate_at(vars(3), as.numeric) %>%
                                           filter(CVE_MUN != CVE_MUN_TRABAJO)  %>%
                                           filter(CVE_MUN %in% ZM[[x]]) %>%
                                            summarize(Emigrantes =  sum(value, na.rm = TRUE)) %>% 
                                             mutate(CVE_ZM = !!paste0(zm[x])) 
})

Emigrantes <- do.call(rbind.data.frame, Emigrantes)

tabla <- Pob.Total %>%
          left_join(., Pob.ocupada, by = c("CVE_ZM")) %>%
          left_join(., Residentes, by = c("CVE_ZM")) %>%
          left_join(., Inmigrantes, by = c("CVE_ZM")) %>%
          left_join(., Emigrantes, by = c("CVE_ZM")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.ocupada) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Intermetropolitano) 2010.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/03_Movilidad laboral/Indicadores de MTrab por ZM 2010 (Intermetropolitano) 2010.RData"))
Indicadores de movilidad laboral (Nivel intermetropolitano)
Zonas Metropolitanas
CVE_ZM Pob.Total Pob.ocupada Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
01.01 930 969 370 756 0 7 327 14 963 −7 636 22 290 2.25 4.60 −2.3 −29 926
02.01 1 730 591 728 958 0 4 615 4 464 151 9 079 0.75 0.73 0.0 −8 928
03.01 932 001 384 781 0 1 835 3 145 −1 310 4 980 0.56 0.96 −0.4 −6 290
04.01 1 214 931 434 981 0 9 465 8 966 499 18 431 2.29 2.17 0.1 −17 932
05.01 820 838 331 678 0 4 996 4 944 52 9 940 1.73 1.72 0.0 −9 888
06.01 316 334 120 770 0 2 456 5 150 −2 694 7 606 2.25 4.71 −2.5 −10 300
07.01 178 886 70 402 0 1 383 3 693 −2 310 5 076 2.22 5.93 −3.7 −7 386
08.01 330 435 148 927 0 7 079 3 774 3 305 10 853 5.91 3.15 2.8 −7 548
09.01 141 869 61 634 0 2 595 4 106 −1 511 6 701 5.10 8.07 −3.0 −8 212
10.01 683 404 280 457 0 12 722 16 825 −4 103 29 547 5.28 6.98 −1.7 −33 650
11.01 1 328 246 516 908 0 1 514 2 706 −1 192 4 220 0.33 0.59 −0.3 −5 412
12.01 847 021 353 754 0 4 463 6 251 −1 788 10 714 1.49 2.08 −0.6 −12 502
13.01 20 014 450 8 415 797 0 114 530 97 233 17 297 211 763 1.61 1.37 0.2 −194 466
14.01 1 604 170 662 785 0 9 951 23 176 −13 225 33 127 1.76 4.09 −2.3 −46 352
15.01 183 266 75 414 0 3 605 5 195 −1 590 8 800 5.57 8.03 −2.5 −10 390
16.01 110 797 44 487 0 921 4 922 −4 001 5 843 2.37 12.68 −10.3 −9 844
17.01 859 676 347 862 0 4 788 4 661 127 9 449 1.59 1.54 0.0 −9 322
18.01 515 075 218 944 0 14 532 18 817 −4 285 33 349 7.92 10.25 −2.3 −37 634
19.01 242 261 95 020 0 5 451 5 382 69 10 833 6.46 6.38 0.1 −10 764
20.01 205 954 78 275 0 6 585 12 779 −6 194 19 364 9.27 17.98 −8.7 −25 558
Fuente: Estimaciones del CONAPO.

Referencias

Librerias que se usaron en el documento

package loadedversion source
Cairo 1.6-1 CRAN (R 4.3.1)
chorddiag 0.1.3 Github ()
circlize 0.4.15 CRAN (R 4.3.1)
doMC 1.3.5 R-Forge (R 4.3.1)
dplyr 1.1.3 CRAN (R 4.3.2)
expss 0.11.6 CRAN (R 4.3.1)
foreach 1.5.2 CRAN (R 4.3.1)
ggalluvial 0.12.5 CRAN (R 4.3.1)
ggplot2 3.4.3 CRAN (R 4.3.1)
ggpubr 0.6.0 CRAN (R 4.3.1)
ggrepel 0.9.3 CRAN (R 4.3.1)
ggsankey 0.0.99999 Github ()
gt 0.10.0 CRAN (R 4.3.1)
haven 2.5.3 CRAN (R 4.3.1)
Hmisc 5.1-0 CRAN (R 4.3.1)
iterators 1.0.14 CRAN (R 4.3.1)
janitor 2.2.0 CRAN (R 4.3.1)
kableExtra 1.3.4 CRAN (R 4.3.1)
knitr 1.45 CRAN (R 4.3.2)
maditr 0.8.3 CRAN (R 4.3.1)
Matrix 1.6-1.1 CRAN (R 4.3.1)
network 1.18.1 CRAN (R 4.3.1)
openxlsx 4.2.5.2 CRAN (R 4.3.1)
reshape2 1.4.4 CRAN (R 4.3.1)
sjlabelled 1.2.0 CRAN (R 4.3.1)
sna 2.7-1 CRAN (R 4.3.1)
srvyr 1.2.0 CRAN (R 4.3.1)
statnet.common 4.9.0 CRAN (R 4.3.1)
stringr 1.5.0 CRAN (R 4.3.1)
survey 4.2 Github ()
survival 3.5-5 CRAN (R 4.3.1)
tibble 3.2.1 CRAN (R 4.3.1)
tidyr 1.3.0 CRAN (R 4.3.1)

Creative Commons Licence
This work by Diana Villasana Ocampo is licensed under a Creative Commons Attribution 4.0 International License.